Вот отредактированный окончательный код
{
"Parameters":{
"ArtifactURL" : {
"Type" : "String",
"Default" : "https://stuff.war",
"Description": "Enter the Artifact's URL"
},
"EC2Name" : {
"Type" : "String",
"Default" : "MyCloudFormationInstance"
}
},
"Resources" : {
"WaitHandle" : {
"Type" : "AWS::CloudFormation::WaitConditionHandle"
},
"MyCloudFormationInstance" : {
"Type" : "AWS::EC2::Instance",
"Metadata" : {
"ArtifactURL": { "Ref": "ArtifactURL"},
"AWS::CloudFormation::Init" : {
"configSets": {
"myconfigs" : ["config-cfn-hup","install-artifact"]
},
"install-artifact" : {
"commands": {
"1-run-script" : {
"command" : { "Fn::Join" : [ "", [
"/bin/bash /etc/downloadandinstallfile.sh"
]]}
}
}
},
"config-cfn-hup" : {
"files" : {
"/etc/downloadandinstallfile.sh" : {
"content" : { "Fn::Join" : ["", [
"#!/bin/bash","\n",
"warlink=\"", { "Ref": "ArtifactURL" },"\"\n",
"echo \"warlink: $warlink\" > myFile.txt","\n",
"\n","curl \"$warlink\" -O","\n"]]},
"mode" : "000777",
"owner" : "root",
"group" : "root"
},
"/etc/cfn/cfn-hup.conf" : {
"content" : { "Fn::Join" : ["", [
"[main]\n",
"stack=", { "Ref" : "AWS::StackId" }, "\n",
"region=", { "Ref" : "AWS::Region" }, "\n",
"interval=1\n"
]]},
"mode" : "000400",
"owner" : "root",
"group" : "root"
},
"/etc/cfn/hooks.d/cfn-auto-reloader.conf" : {
"content" : { "Fn::Join" : ["", [
"[cfn-auto-reloader-hook]\n",
"triggers=post.update\n",
"path=Resources.MyCloudFormationInstance.Metadata.AWS::CloudFormation::Init\n",
"action=cfn-init -v --configsets myconfigs --stack ", { "Ref" : "AWS::StackId" }, " --resource MyCloudFormationInstance ", " --region ", { "Ref" : "AWS::Region"},"\n",
"runas=root\n"
]]}
},
"/etc/systemd/system/cfn-hup.service" : {
"content" : { "Fn::Join" : ["", [
"[Unit]\n",
"Description=CloudFormation helper daemon\n",
"\n",
"[Service]\n",
"ExecStart=/bin/cfn-hup\n",
"Restart=always\n",
"Type=simple\n",
"\n",
"[Install]\n",
"WantedBy=multi-user.target"
]]}
}
},
"commands" : {
"01-install-set" : {
"command" : { "Fn::Join" : [ "", [
"set -xe\n"
]]}
},
"02-daemon-reload" : {
"command" : { "Fn::Join" : [ "", [
"systemctl daemon-reload"
]]}
},
"03-enable-service" : {
"command" : { "Fn::Join" : [ "", [
"systemctl enable cfn-hup.service\n"
]]}
},
"04-start-service" : {
"command" : { "Fn::Join" : [ "", [
"service cfn-hup.service start\n"
]]}
}
}
}
}
},
"Properties" : {
"AvailabilityZone" : "xxxxxxxx",
"ImageId" : "xxxxxxxxxx",
"SecurityGroupIds" :[ "xxxxxxx" ],
"KeyName" : "xxx.pem",
"Tags" : [
{
"Key" : "Name",
"Value" : { "Ref": "EC2Name"}
},
{
"Key" : "Url",
"Value" : { "Ref": "NexusArtifactURL"}
}
],
"IamInstanceProfile" : "ec2LoggingRole",
"InstanceType" : "t2.micro",
"UserData" : { "Fn::Base64" : { "Fn::Join" : ["", [
"#!/bin/bash -v\n",
"# Function to return error code to the wait handle\n",
"function handle_error\n",
"{\n",
" cfn-signal -e 1 -r \"$1\" '", { "Ref" : "WaitHandle"}, "'\n",
" exit 1\n",
"}\n",
"yum update -y \n",
"pip3 install pystache\n",
"pip3 install argparse\n",
"pip3 install python-daemon\n",
"pip3 install requests\n",
"curl -sSL https://s3.amazonaws.com/cloudformation-examples/aws-cfn-bootstrap-latest.tar.gz -O\n",
"# tar -xpf aws-cfn-bootstrap-latest.tar.gz -C /opt\n",
"# cd /opt/aws-cfn-bootstrap-1.4/\n",
"# sudo python setup.py build\n",
"# sudo python setup.py install\n",
"easy_install aws-cfn-bootstrap-latest.tar.gz\n",
"#ln -s /usr/init/redhat/cfn-hup /etc/init.d/cfn-hup\n",
"#chmod 775 /usr/init/redhat/cfn-hup\n",
"#cd /opt\n",
"#mkdir aws\n",
"#cd aws\n",
"#mkdir bin\n",
"#ln -s /usr/bin/cfn-hup /opt/aws/bin/cfn-hup\n",
"sudo cfn-init -v -c myconfigs -s ", { "Ref" : "AWS::StackId" }, " -r MyCloudFormationInstance ", " --region ", { "Ref" : "AWS::Region" }, " || handle_error 'Failed to run cfn-init'\n",
"# Return success\n",
"cfn-signal -e 0 -r \"Stack Complete\" '", { "Ref" : "WaitHandle" }, "'\n"
]]}
}
}
}
},
"Outputs":{
"InstanceID" : {
"Description" : "The Instance ID",
"Value": { "Ref" : "MyCloudFormationInstance" }
},
"PublicIp" : {
"Description" : "Instance's Public Ip",
"Value" : { "Fn::GetAtt" : [ "MyCloudFormationInstance", "PublicIp"]}
},
"SSHLogin" : {
"Description" : "SSH command to log into your instance",
"Value" : {"Fn::Join" : ["", ["ssh -i \"xxx.pem\" ec2-user@", {"Fn::GetAtt": ["MyCloudFormationInstance","PublicDnsName"]}]]}
}
}}