在配置AWS cloudwatch代理时,您可以将其包含在{instance_id}
内的awslogs.conf
中
[/var/log/cfn-hup.log]
file = /var/log/cfn-hup.log
log_group_name = my-log-group
log_stream_name = {instance_id}/cfn-hup
是否可以在AWS::CloudFormation::Init内执行此操作?
具体地说,我想在files部分添加实例ID
Resources:
MyLaunchConfig:
Type: AWS::AutoScaling::LaunchConfiguration
Metadata:
AWS::CloudFormation::Init:
config:
files:
/opt/someconfig.conf:
content: |
INSTANCE_ID={instance_id}
mode: "000644"
owner: "root"
group: "root"
我的解决方法是通过运行以下命令,将INSTANCE_ID
命令后的cfn-init
附加到我的配置文件 中:
echo "INSTANCE_ID=$(curl http://169.254.169.254/latest/meta-data/instance-id)" >> /opt/someconfig.conf
有更好的方法吗?
答案 0 :(得分:0)
我接受了Marcin在评论中指出的方法
Resources:
MyLaunchConfig:
Type: AWS::AutoScaling::LaunchConfiguration
Metadata:
AWS::CloudFormation::Init:
config:
files:
/opt/someconfig.conf:
content: |
INSTANCE_ID='INSTANCE_ID_PLACEHOLDER'
mode: "000644"
owner: "root"
Properties:
# removed for brevity
UserData:
Fn::Base64: !Sub |
#!/bin/bash -xe
cfn-init -v \
--stack ${AWS::StackName} \
--resource MyLaunchConfig \
--region ${AWS::Region}
INSTANCE_ID=$(curl http://169.254.169.254/latest/meta-data/instance-id)
sed -i "s/INSTANCE_ID_PLACEHOLDER/$INSTANCE_ID/g" /opt/someconfig.conf