gpt4 book ai didi

amazon-web-services - 如何使用 CloudFormation 自动标记根设备卷

转载 作者:行者123 更新时间:2023-12-04 04:04:23 24 4
gpt4 key购买 nike

我无法 tag root device volume附于EC2使用CloudFormationblock device mapping因为tags不会传播到亚马逊EBSblock device mappings 创建的卷。可以root device volumetagging使用Cloudformation自动化以任何方式?谢谢。

最佳答案

云形成

这刚刚在 CloudFormation 中发布,可通过属性设置

传播标签到卷创建

https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-ec2-instance.html#cfn-ec2-instance-propagatetagstovolumeoncreation

用户数据

可以使用UserData来做到这一点- 如果您正在使用 cloudinit 和 awscli installed 运行 Linux 主机,可以在 UserData 中运行以下命令用于标记与实例关联的所有卷的脚本

"VOLUME_IDS=$(aws ec2 describe-volumes --output text --filters Name=attachment.instance-id,Values=$(curl http://169.254.169.254/latest/meta-data/instance-id) --query 'Volumes[].VolumeId')",
"aws ec2 create-tags --resources ${VOLUME_IDS} --tags Key=my,Value=tag"

确保当您启动 EC2 实例时,它具有实例 IAM 策略,使其能够创建标签和描述卷

"PolicyDocument": {
"Version": "2012-10-17",
"Statement": [
{
"Action": [
"ec2:CreateTags",
"ec2:DescribeVolumes"
],
"Effect": "Allow",
"Resource": "*"
}
]
}

## CloudWatch 事件自动化此操作的另一种方法是通过 CloudWatch Events,设置一个事件规则监听和 EC2 状态更改,然后标记 Lambda 函数中的卷,我在下面包含了几个 CloudFormation 片段

LambdaEC2CopyTagsToEBS:
Type: AWS::IAM::Role
Properties:
AssumeRolePolicyDocument:
Version: 2012-10-17
Statement:
- Effect: Allow
Principal:
Service:
- lambda.amazonaws.com
Action:
- sts:AssumeRole
Policies:
- PolicyName: LambdaEC2CopyTagsToEBS
PolicyDocument:
Version: 2012-10-17
Statement:
- Effect: Allow
Action:
- ec2:DescribeInstances
- ec2:CreateTags
Resource: '*'

- Effect: Allow
Action:
- logs:CreateLogGroup
- logs:CreateLogStream
- logs:PutLogEvents
Resource: '*'

LambdaEC2CopyTagsToEBSEvent:
Type: AWS::Events::Rule
Properties:
Description: Invokes CopyInstanceTagsToEBSVolumes when an Instance starts running
EventPattern:
source:
- aws.ec2
detail-type:
- EC2 Instance State-change Notification
detail:
state:
- running
State: ENABLED
Targets:
- Arn: !GetAtt CopyInstanceTagsToEBSVolumes.Arn
Id: !Ref CopyInstanceTagsToEBSVolumes

CopyInstanceTagsToEBSVolumes:
Type: AWS::Lambda::Function
Properties:
Description: Copies Tags from and EC2 to all its EBS Volumes
Code:
ZipFile: |
import boto3
ec2 = boto3.client('ec2')


def get_volume_ids(instance):
for device in instance.get('BlockDeviceMappings', []):
yield device.get('Ebs', {}).get('VolumeId')


def handler(event, context):
state, instance_id = event['detail']['state'], event['detail']['instance-id']
if state == 'running':
instance = ec2.describe_instances(InstanceIds=[instance_id])
instance = instance['Reservations'][0]['Instances'][0]
volume_ids = get_volume_ids(instance)
tags = [tag for tag in instance['Tags'] if not tag['Key'].startswith('aws:')]
ec2.create_tags(Resources=list(volume_ids),
Tags=tags
)

Handler: index.handler
Role: !GetAtt LambdaEC2CopyTagsToEBS.Arn
Runtime: python3.6
Timeout: 5


EventsInvokeCopyInstanceTagsToEBSVolumes:
Type: AWS::Lambda::Permission
Properties:
Action: lambda:InvokeFunction
FunctionName: !Ref CopyInstanceTagsToEBSVolumes
Principal: events.amazonaws.com
SourceArn: !GetAtt LambdaEC2CopyTagsToEBSEvent.Arn

关于amazon-web-services - 如何使用 CloudFormation 自动标记根设备卷,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33622558/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com