Skip to content

Instantly share code, notes, and snippets.

@sclausson
Last active December 28, 2016 13:30
Show Gist options
  • Save sclausson/f93beb521ba8675f69a452c8fe6e9c0d to your computer and use it in GitHub Desktop.
Save sclausson/f93beb521ba8675f69a452c8fe6e9c0d to your computer and use it in GitHub Desktop.
{
"Description": "Create instances ready for CodeDeploy: Create 3 to 5 Amazon EC2 instances with an associated instance profile and install the AWS CodeDeploy Agent. **WARNING** This template creates one or more Amazon EC2 instances. You will be billed for the AWS resources used if you create a stack from this template. Copyright [2012-2014] Amazon.com, Inc. or its affiliates. All Rights Reserved. Licensed under the Apache License, Version 2.0 (the \"License\"). You may not use this file except in compliance with the License. A copy of the License is located at http://aws.amazon.com/apache2.0/ or in the \"license\" file accompanying this file. This file is distributed on an \"AS IS\" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.",
"AWSTemplateFormatVersion": "2010-09-09",
"Parameters": {
"TagKey": {
"Description": "The EC2 tag key that identifies this as a target for deployments.",
"Type": "String",
"Default": "Name",
"AllowedPattern": "[\\x20-\\x7E]*",
"ConstraintDescription": "Can contain only ASCII characters."
},
"TagValue": {
"Description": "The EC2 tag value that identifies this as a target for deployments.",
"Type": "String",
"Default": "CodeDeployDemo",
"AllowedPattern": "[\\x20-\\x7E]*",
"ConstraintDescription": "Can contain only ASCII characters."
},
"KeyPairName": {
"Description":"Name of an existing Amazon EC2 key pair to enable SSH or RDP access to the instances.",
"Type":"AWS::EC2::KeyPair::KeyName",
"MinLength":"1",
"MaxLength":"255",
"AllowedPattern":"[\\x20-\\x7E]*",
"ConstraintDescription":"Can contain only ASCII characters."
},
"InstanceType": {
"Description": "Amazon EC2 instance type.",
"Type": "String",
"Default": "c3.xlarge",
"ConstraintDescription": "Must be a valid Amazon EC2 instance type."
},
"InstanceCount": {
"Description": "Number of Amazon EC2 instances (Must be a number between 3 and 5).",
"Type": "Number",
"Default": "3",
"ConstraintDescription": "Must be a number between 3 and 5.",
"MinValue": "3",
"MaxValue": "5"
},
"OperatingSystem": {
"Description": "Amazon EC2 operating system type (Linux or Windows).",
"Type": "String",
"Default": "Linux",
"ConstraintDescription": "Must be Windows or Linux.",
"AllowedValues": [
"Linux",
"Windows"
]
},
"SSHLocation": {
"Description": "The IP address range that can be used to connect using SSH or RDP to the Amazon EC2 instances.",
"Type": "String",
"MinLength": "9",
"MaxLength": "18",
"Default": "0.0.0.0/0",
"AllowedPattern": "(\\d{1,3})\\.(\\d{1,3})\\.(\\d{1,3})\\.(\\d{1,3})/(\\d{1,2})",
"ConstraintDescription": "Must be a valid IP CIDR range of the form x.x.x.x/x."
}
},
"Mappings": {
"RegionOS2AMI": {
"us-east-1": {
"Linux": "ami-246ed34c",
"Windows": "ami-ac3a1cc4"
},
"us-west-2": {
"Linux": "ami-55a7ea65",
"Windows": "ami-7f634e4f"
}
},
"OS2SSHPort": {
"Linux": {
"SSHPort": "22"
},
"Windows": {
"SSHPort": "3389"
}
}
},
"Conditions": {
"LaunchInstance4": {
"Fn::Or": [
{
"Fn::Equals": [
"4",
{
"Ref": "InstanceCount"
}
]
},
{
"Fn::Equals": [
"5",
{
"Ref": "InstanceCount"
}
]
}
]
},
"LaunchInstance5": {
"Fn::Equals": [
"5",
{
"Ref": "InstanceCount"
}
]
},
"LaunchLinuxEC2Instance3": {
"Fn::Equals": [
{
"Ref": "OperatingSystem"
},
"Linux"
]
},
"LaunchLinuxEC2Instance4": {
"Fn::And": [
{
"Condition": "LaunchLinuxEC2Instance3"
},
{
"Condition": "LaunchInstance4"
}
]
},
"LaunchLinuxEC2Instance5": {
"Fn::And": [
{
"Condition": "LaunchLinuxEC2Instance3"
},
{
"Condition": "LaunchInstance5"
}
]
},
"LaunchWindowsEC2Instance3": {
"Fn::Equals": [
{
"Ref": "OperatingSystem"
},
"Windows"
]
},
"LaunchWindowsEC2Instance4": {
"Fn::And": [
{
"Condition": "LaunchWindowsEC2Instance3"
},
{
"Condition": "LaunchInstance4"
}
]
},
"LaunchWindowsEC2Instance5": {
"Fn::And": [
{
"Condition": "LaunchWindowsEC2Instance3"
},
{
"Condition": "LaunchInstance5"
}
]
}
},
"Resources": {
"LinuxEC2Instance1": {
"Type": "AWS::EC2::Instance",
"Condition": "LaunchLinuxEC2Instance3",
"Metadata": {
"AWS::CloudFormation::Init": {
"services": {
"sysvint": {
"codedeploy-agent": {
"enabled": "true",
"ensureRunning": "true"
}
}
}
}
},
"Properties": {
"ImageId": {
"Fn::FindInMap": [
"RegionOS2AMI",
{
"Ref": "AWS::Region"
},
{
"Ref": "OperatingSystem"
}
]
},
"InstanceType": {
"Ref": "InstanceType"
},
"SecurityGroups": [
{
"Ref": "SecurityGroup"
}
],
"UserData": {
"Fn::Base64": {
"Fn::Join": [
"",
[
"#!/bin/bash -ex\n",
"yum update -y aws-cfn-bootstrap\n",
"yum install -y aws-cli\n",
"# Helper function.\n",
"function error_exit\n",
"{\n",
" /opt/aws/bin/cfn-signal -e 1 -r \"$1\" '",
{
"Ref": "WaitHandle"
},
"'\n",
" exit 1\n",
"}\n",
"# Install the AWS CodeDeploy Agent.\n",
"cd /home/ec2-user/\n",
"aws s3 cp 's3://aws-codedeploy-us-east-1/latest/codedeploy-agent.noarch.rpm' . || error_exit 'Failed to download AWS CodeDeploy Agent.'\n",
"yum -y install codedeploy-agent.noarch.rpm || error_exit 'Failed to install AWS CodeDeploy Agent.' \n",
"/opt/aws/bin/cfn-init -s ",
{
"Ref": "AWS::StackId"
},
" -r LinuxEC2Instance --region ",
{
"Ref": "AWS::Region"
},
" || error_exit 'Failed to run cfn-init.'\n",
"# All is well, so signal success.\n",
"/opt/aws/bin/cfn-signal -e 0 -r \"AWS CodeDeploy Agent setup complete.\" '",
{
"Ref": "WaitHandle"
},
"'\n"
]
]
}
},
"KeyName": {
"Ref": "KeyPairName"
},
"Tags": [
{
"Key": {
"Ref": "TagKey"
},
"Value": {
"Ref": "TagValue"
}
}
],
"IamInstanceProfile": {
"Ref": "InstanceRoleInstanceProfile"
}
}
},
"LinuxEC2Instance2": {
"Type": "AWS::EC2::Instance",
"Condition": "LaunchLinuxEC2Instance3",
"Metadata": {
"AWS::CloudFormation::Init": {
"services": {
"sysvint": {
"codedeploy-agent": {
"enabled": "true",
"ensureRunning": "true"
}
}
}
}
},
"Properties": {
"ImageId": {
"Fn::FindInMap": [
"RegionOS2AMI",
{
"Ref": "AWS::Region"
},
{
"Ref": "OperatingSystem"
}
]
},
"InstanceType": {
"Ref": "InstanceType"
},
"SecurityGroups": [
{
"Ref": "SecurityGroup"
}
],
"UserData": {
"Fn::Base64": {
"Fn::Join": [
"",
[
"#!/bin/bash -ex\n",
"yum update -y aws-cfn-bootstrap\n",
"yum install -y aws-cli\n",
"# Helper function.\n",
"function error_exit\n",
"{\n",
" /opt/aws/bin/cfn-signal -e 1 -r \"$1\" '",
{
"Ref": "WaitHandle"
},
"'\n",
" exit 1\n",
"}\n",
"# Install the AWS CodeDeploy Agent.\n",
"cd /home/ec2-user/\n",
"aws s3 cp 's3://aws-codedeploy-us-east-1/latest/codedeploy-agent.noarch.rpm' . || error_exit 'Failed to download AWS CodeDeploy Agent.'\n",
"yum -y install codedeploy-agent.noarch.rpm || error_exit 'Failed to install AWS CodeDeploy Agent.' \n",
"/opt/aws/bin/cfn-init -s ",
{
"Ref": "AWS::StackId"
},
" -r LinuxEC2Instance --region ",
{
"Ref": "AWS::Region"
},
" || error_exit 'Failed to run cfn-init.'\n",
"# All is well, so signal success.\n",
"/opt/aws/bin/cfn-signal -e 0 -r \"AWS CodeDeploy Agent setup complete.\" '",
{
"Ref": "WaitHandle"
},
"'\n"
]
]
}
},
"KeyName": {
"Ref": "KeyPairName"
},
"Tags": [
{
"Key": {
"Ref": "TagKey"
},
"Value": {
"Ref": "TagValue"
}
}
],
"IamInstanceProfile": {
"Ref": "InstanceRoleInstanceProfile"
}
}
},
"LinuxEC2Instance3": {
"Type": "AWS::EC2::Instance",
"Condition": "LaunchLinuxEC2Instance3",
"Metadata": {
"AWS::CloudFormation::Init": {
"services": {
"sysvint": {
"codedeploy-agent": {
"enabled": "true",
"ensureRunning": "true"
}
}
}
}
},
"Properties": {
"ImageId": {
"Fn::FindInMap": [
"RegionOS2AMI",
{
"Ref": "AWS::Region"
},
{
"Ref": "OperatingSystem"
}
]
},
"InstanceType": {
"Ref": "InstanceType"
},
"SecurityGroups": [
{
"Ref": "SecurityGroup"
}
],
"UserData": {
"Fn::Base64": {
"Fn::Join": [
"",
[
"#!/bin/bash -ex\n",
"yum update -y aws-cfn-bootstrap\n",
"yum install -y aws-cli\n",
"# Helper function.\n",
"function error_exit\n",
"{\n",
" /opt/aws/bin/cfn-signal -e 1 -r \"$1\" '",
{
"Ref": "WaitHandle"
},
"'\n",
" exit 1\n",
"}\n",
"# Install the AWS CodeDeploy Agent.\n",
"cd /home/ec2-user/\n",
"aws s3 cp 's3://aws-codedeploy-us-east-1/latest/codedeploy-agent.noarch.rpm' . || error_exit 'Failed to download AWS CodeDeploy Agent.'\n",
"yum -y install codedeploy-agent.noarch.rpm || error_exit 'Failed to install AWS CodeDeploy Agent.' \n",
"/opt/aws/bin/cfn-init -s ",
{
"Ref": "AWS::StackId"
},
" -r LinuxEC2Instance --region ",
{
"Ref": "AWS::Region"
},
" || error_exit 'Failed to run cfn-init.'\n",
"# All is well, so signal success.\n",
"/opt/aws/bin/cfn-signal -e 0 -r \"AWS CodeDeploy Agent setup complete.\" '",
{
"Ref": "WaitHandle"
},
"'\n"
]
]
}
},
"KeyName": {
"Ref": "KeyPairName"
},
"Tags": [
{
"Key": {
"Ref": "TagKey"
},
"Value": {
"Ref": "TagValue"
}
}
],
"IamInstanceProfile": {
"Ref": "InstanceRoleInstanceProfile"
}
}
},
"WindowsEC2Instance1": {
"Type": "AWS::EC2::Instance",
"Condition": "LaunchWindowsEC2Instance3",
"Metadata": {
"AWS::CloudFormation::Init": {
"config": {
"commands": {
"00-download-host-agent": {
"command": {
"Fn::Join": [
"",
[
"powershell.exe -Command \"Read-S3Object ",
"-BucketName aws-codedeploy-us-east-1 ",
"-Key latest/codedeploy-agent.msi ",
"-File codedeploy-agent.msi\""
]
]
},
"cwd": "C:/cfn",
"waitAfterCompletion": 0
},
"01-install-host-agent": {
"command": "C:\\cfn\\codedeploy-agent.msi /quiet /l C:\\cfn\\host-agent-install-log.txt",
"ignoreErrors": "true",
"waitAfterCompletion": 0
},
"02-signal-ready": {
"command": {
"Fn::Join": [
"",
[
"\"C:\\Program Files\\Amazon\\cfn-bootstrap\\cfn-signal\"",
" -e 0 \"",
{
"Ref": "WaitHandle"
},
"\""
]
]
},
"waitAfterCompletion": 0
}
},
"services": {
"windows": {
"codedeploy-agent": {
"enabled": "true",
"ensureRunning": "true",
"commands": [
"01-install-host-agent"
]
}
}
}
}
}
},
"Properties": {
"ImageId": {
"Fn::FindInMap": [
"RegionOS2AMI",
{
"Ref": "AWS::Region"
},
{
"Ref": "OperatingSystem"
}
]
},
"InstanceType": {
"Ref": "InstanceType"
},
"SecurityGroups": [
{
"Ref": "SecurityGroup"
}
],
"UserData": {
"Fn::Base64": {
"Fn::Join": [
"",
[
"<script>\n",
"mkdir c:\\cfn\n",
"mkdir c:\\cfn\\log\n",
"c:\\\"Program Files\"\\Amazon\\cfn-bootstrap\\cfn-init.exe -s ",
{
"Ref": "AWS::StackName"
},
" --region ",
{
"Ref": "AWS::Region"
},
" -r WindowsEC2Instance > c:\\cfn\\log\\cfn-call-log 2>&1",
"</script>"
]
]
}
},
"KeyName": {
"Ref": "KeyPairName"
},
"Tags": [
{
"Key": {
"Ref": "TagKey"
},
"Value": {
"Ref": "TagValue"
}
}
],
"IamInstanceProfile": {
"Ref": "InstanceRoleInstanceProfile"
}
}
},
"WindowsEC2Instance2": {
"Type": "AWS::EC2::Instance",
"Condition": "LaunchWindowsEC2Instance3",
"Metadata": {
"AWS::CloudFormation::Init": {
"config": {
"commands": {
"00-download-host-agent": {
"command": {
"Fn::Join": [
"",
[
"powershell.exe -Command \"Read-S3Object ",
"-BucketName aws-codedeploy-us-east-1 ",
"-Key latest/codedeploy-agent.msi ",
"-File codedeploy-agent.msi\""
]
]
},
"cwd": "C:/cfn",
"waitAfterCompletion": 0
},
"01-install-host-agent": {
"command": "C:\\cfn\\codedeploy-agent.msi /quiet /l C:\\cfn\\host-agent-install-log.txt",
"ignoreErrors": "true",
"waitAfterCompletion": 0
},
"02-signal-ready": {
"command": {
"Fn::Join": [
"",
[
"\"C:\\Program Files\\Amazon\\cfn-bootstrap\\cfn-signal\"",
" -e 0 \"",
{
"Ref": "WaitHandle"
},
"\""
]
]
},
"waitAfterCompletion": 0
}
},
"services": {
"windows": {
"codedeploy-agent": {
"enabled": "true",
"ensureRunning": "true",
"commands": [
"01-install-host-agent"
]
}
}
}
}
}
},
"Properties": {
"ImageId": {
"Fn::FindInMap": [
"RegionOS2AMI",
{
"Ref": "AWS::Region"
},
{
"Ref": "OperatingSystem"
}
]
},
"InstanceType": {
"Ref": "InstanceType"
},
"SecurityGroups": [
{
"Ref": "SecurityGroup"
}
],
"UserData": {
"Fn::Base64": {
"Fn::Join": [
"",
[
"<script>\n",
"mkdir c:\\cfn\n",
"mkdir c:\\cfn\\log\n",
"c:\\\"Program Files\"\\Amazon\\cfn-bootstrap\\cfn-init.exe -s ",
{
"Ref": "AWS::StackName"
},
" --region ",
{
"Ref": "AWS::Region"
},
" -r WindowsEC2Instance > c:\\cfn\\log\\cfn-call-log 2>&1",
"</script>"
]
]
}
},
"KeyName": {
"Ref": "KeyPairName"
},
"Tags": [
{
"Key": {
"Ref": "TagKey"
},
"Value": {
"Ref": "TagValue"
}
}
],
"IamInstanceProfile": {
"Ref": "InstanceRoleInstanceProfile"
}
}
},
"WindowsEC2Instance3": {
"Type": "AWS::EC2::Instance",
"Condition": "LaunchWindowsEC2Instance3",
"Metadata": {
"AWS::CloudFormation::Init": {
"config": {
"commands": {
"00-download-host-agent": {
"command": {
"Fn::Join": [
"",
[
"powershell.exe -Command \"Read-S3Object ",
"-BucketName aws-codedeploy-us-east-1 ",
"-Key latest/codedeploy-agent.msi ",
"-File codedeploy-agent.msi\""
]
]
},
"cwd": "C:/cfn",
"waitAfterCompletion": 0
},
"01-install-host-agent": {
"command": "C:\\cfn\\codedeploy-agent.msi /quiet /l C:\\cfn\\host-agent-install-log.txt",
"ignoreErrors": "true",
"waitAfterCompletion": 0
},
"02-signal-ready": {
"command": {
"Fn::Join": [
"",
[
"\"C:\\Program Files\\Amazon\\cfn-bootstrap\\cfn-signal\"",
" -e 0 \"",
{
"Ref": "WaitHandle"
},
"\""
]
]
},
"waitAfterCompletion": 0
}
},
"services": {
"windows": {
"codedeploy-agent": {
"enabled": "true",
"ensureRunning": "true",
"commands": [
"01-install-host-agent"
]
}
}
}
}
}
},
"Properties": {
"ImageId": {
"Fn::FindInMap": [
"RegionOS2AMI",
{
"Ref": "AWS::Region"
},
{
"Ref": "OperatingSystem"
}
]
},
"InstanceType": {
"Ref": "InstanceType"
},
"SecurityGroups": [
{
"Ref": "SecurityGroup"
}
],
"UserData": {
"Fn::Base64": {
"Fn::Join": [
"",
[
"<script>\n",
"mkdir c:\\cfn\n",
"mkdir c:\\cfn\\log\n",
"c:\\\"Program Files\"\\Amazon\\cfn-bootstrap\\cfn-init.exe -s ",
{
"Ref": "AWS::StackName"
},
" --region ",
{
"Ref": "AWS::Region"
},
" -r WindowsEC2Instance > c:\\cfn\\log\\cfn-call-log 2>&1",
"</script>"
]
]
}
},
"KeyName": {
"Ref": "KeyPairName"
},
"Tags": [
{
"Key": {
"Ref": "TagKey"
},
"Value": {
"Ref": "TagValue"
}
}
],
"IamInstanceProfile": {
"Ref": "InstanceRoleInstanceProfile"
}
}
},
"WaitHandle": {
"Type": "AWS::CloudFormation::WaitConditionHandle"
},
"WaitCondition": {
"Type": "AWS::CloudFormation::WaitCondition",
"Properties": {
"Count": {
"Ref": "InstanceCount"
},
"Handle": {
"Ref": "WaitHandle"
},
"Timeout": "900"
}
},
"SecurityGroup": {
"Type": "AWS::EC2::SecurityGroup",
"Properties": {
"GroupDescription": "Enable HTTP access via port 80 and SSH access.",
"SecurityGroupIngress": [
{
"IpProtocol": "tcp",
"FromPort": "80",
"ToPort": "80",
"CidrIp": "0.0.0.0/0"
},
{
"IpProtocol": "tcp",
"FromPort": {
"Fn::FindInMap": [
"OS2SSHPort",
{
"Ref": "OperatingSystem"
},
"SSHPort"
]
},
"ToPort": {
"Fn::FindInMap": [
"OS2SSHPort",
{
"Ref": "OperatingSystem"
},
"SSHPort"
]
},
"CidrIp": {
"Ref": "SSHLocation"
}
}
]
}
},
"CodeDeployTrustRole": {
"Type": "AWS::IAM::Role",
"Properties": {
"AssumeRolePolicyDocument": {
"Statement": [
{
"Sid": "1",
"Effect": "Allow",
"Principal": {
"Service": [
"codedeploy.us-east-1.amazonaws.com",
"codedeploy.us-west-2.amazonaws.com"
]
},
"Action": "sts:AssumeRole"
}
]
},
"Path": "/"
}
},
"CodeDeployRolePolicies": {
"Type": "AWS::IAM::Policy",
"Properties": {
"PolicyName": "CodeDeployPolicy",
"PolicyDocument": {
"Statement": [
{
"Effect": "Allow",
"Resource": [
"*"
],
"Action": [
"ec2:Describe*"
]
},
{
"Effect": "Allow",
"Resource": [
"*"
],
"Action": [
"autoscaling:CompleteLifecycleAction",
"autoscaling:DeleteLifecycleHook",
"autoscaling:DescribeLifecycleHooks",
"autoscaling:DescribeAutoScalingGroups",
"autoscaling:PutLifecycleHook",
"autoscaling:RecordLifecycleActionHeartbeat"
]
}
]
},
"Roles": [
{
"Ref": "CodeDeployTrustRole"
}
]
}
},
"InstanceRole": {
"Type": "AWS::IAM::Role",
"Properties": {
"AssumeRolePolicyDocument": {
"Statement": [
{
"Effect": "Allow",
"Principal": {
"Service": [
"ec2.amazonaws.com"
]
},
"Action": [
"sts:AssumeRole"
]
}
]
},
"Path": "/"
}
},
"InstanceRolePolicies": {
"Type": "AWS::IAM::Policy",
"Properties": {
"PolicyName": "InstanceRole",
"PolicyDocument": {
"Statement": [
{
"Effect": "Allow",
"Action": [
"autoscaling:Describe*",
"cloudformation:Describe*",
"cloudformation:GetTemplate",
"s3:Get*"
],
"Resource": "*"
}
]
},
"Roles": [
{
"Ref": "InstanceRole"
}
]
}
},
"InstanceRoleInstanceProfile": {
"Type": "AWS::IAM::InstanceProfile",
"Properties": {
"Path": "/",
"Roles": [
{
"Ref": "InstanceRole"
}
]
}
},
"LinuxEC2Instance4": {
"Type": "AWS::EC2::Instance",
"Condition": "LaunchLinuxEC2Instance4",
"Metadata": {
"AWS::CloudFormation::Init": {
"services": {
"sysvint": {
"codedeploy-agent": {
"enabled": "true",
"ensureRunning": "true"
}
}
}
}
},
"Properties": {
"ImageId": {
"Fn::FindInMap": [
"RegionOS2AMI",
{
"Ref": "AWS::Region"
},
{
"Ref": "OperatingSystem"
}
]
},
"InstanceType": {
"Ref": "InstanceType"
},
"SecurityGroups": [
{
"Ref": "SecurityGroup"
}
],
"UserData": {
"Fn::Base64": {
"Fn::Join": [
"",
[
"#!/bin/bash -ex\n",
"yum update -y aws-cfn-bootstrap\n",
"yum install -y aws-cli\n",
"# Helper function.\n",
"function error_exit\n",
"{\n",
" /opt/aws/bin/cfn-signal -e 1 -r \"$1\" '",
{
"Ref": "WaitHandle"
},
"'\n",
" exit 1\n",
"}\n",
"# Install the AWS CodeDeploy Agent.\n",
"cd /home/ec2-user/\n",
"aws s3 cp 's3://aws-codedeploy-us-east-1/latest/codedeploy-agent.noarch.rpm' . || error_exit 'Failed to download AWS CodeDeploy Agent.'\n",
"yum -y install codedeploy-agent.noarch.rpm || error_exit 'Failed to install AWS CodeDeploy Agent.' \n",
"/opt/aws/bin/cfn-init -s ",
{
"Ref": "AWS::StackId"
},
" -r LinuxEC2Instance --region ",
{
"Ref": "AWS::Region"
},
" || error_exit 'Failed to run cfn-init.'\n",
"# All is well, so signal success.\n",
"/opt/aws/bin/cfn-signal -e 0 -r \"AWS CodeDeploy Agent setup complete.\" '",
{
"Ref": "WaitHandle"
},
"'\n"
]
]
}
},
"KeyName": {
"Ref": "KeyPairName"
},
"Tags": [
{
"Key": {
"Ref": "TagKey"
},
"Value": {
"Ref": "TagValue"
}
}
],
"IamInstanceProfile": {
"Ref": "InstanceRoleInstanceProfile"
}
}
},
"WindowsEC2Instance4": {
"Type": "AWS::EC2::Instance",
"Condition": "LaunchWindowsEC2Instance4",
"Metadata": {
"AWS::CloudFormation::Init": {
"config": {
"commands": {
"00-download-host-agent": {
"command": {
"Fn::Join": [
"",
[
"powershell.exe -Command \"Read-S3Object ",
"-BucketName aws-codedeploy-us-east-1 ",
"-Key latest/codedeploy-agent.msi ",
"-File codedeploy-agent.msi\""
]
]
},
"cwd": "C:/cfn",
"waitAfterCompletion": 0
},
"01-install-host-agent": {
"command": "C:\\cfn\\codedeploy-agent.msi /quiet /l C:\\cfn\\host-agent-install-log.txt",
"ignoreErrors": "true",
"waitAfterCompletion": 0
},
"02-signal-ready": {
"command": {
"Fn::Join": [
"",
[
"\"C:\\Program Files\\Amazon\\cfn-bootstrap\\cfn-signal\"",
" -e 0 \"",
{
"Ref": "WaitHandle"
},
"\""
]
]
},
"waitAfterCompletion": 0
}
},
"services": {
"windows": {
"codedeploy-agent": {
"enabled": "true",
"ensureRunning": "true",
"commands": [
"01-install-host-agent"
]
}
}
}
}
}
},
"Properties": {
"ImageId": {
"Fn::FindInMap": [
"RegionOS2AMI",
{
"Ref": "AWS::Region"
},
{
"Ref": "OperatingSystem"
}
]
},
"InstanceType": {
"Ref": "InstanceType"
},
"SecurityGroups": [
{
"Ref": "SecurityGroup"
}
],
"UserData": {
"Fn::Base64": {
"Fn::Join": [
"",
[
"<script>\n",
"mkdir c:\\cfn\n",
"mkdir c:\\cfn\\log\n",
"c:\\\"Program Files\"\\Amazon\\cfn-bootstrap\\cfn-init.exe -s ",
{
"Ref": "AWS::StackName"
},
" --region ",
{
"Ref": "AWS::Region"
},
" -r WindowsEC2Instance > c:\\cfn\\log\\cfn-call-log 2>&1",
"</script>"
]
]
}
},
"KeyName": {
"Ref": "KeyPairName"
},
"Tags": [
{
"Key": {
"Ref": "TagKey"
},
"Value": {
"Ref": "TagValue"
}
}
],
"IamInstanceProfile": {
"Ref": "InstanceRoleInstanceProfile"
}
}
},
"LinuxEC2Instance5": {
"Type": "AWS::EC2::Instance",
"Condition": "LaunchLinuxEC2Instance5",
"Metadata": {
"AWS::CloudFormation::Init": {
"services": {
"sysvint": {
"codedeploy-agent": {
"enabled": "true",
"ensureRunning": "true"
}
}
}
}
},
"Properties": {
"ImageId": {
"Fn::FindInMap": [
"RegionOS2AMI",
{
"Ref": "AWS::Region"
},
{
"Ref": "OperatingSystem"
}
]
},
"InstanceType": {
"Ref": "InstanceType"
},
"SecurityGroups": [
{
"Ref": "SecurityGroup"
}
],
"UserData": {
"Fn::Base64": {
"Fn::Join": [
"",
[
"#!/bin/bash -ex\n",
"yum update -y aws-cfn-bootstrap\n",
"yum install -y aws-cli\n",
"# Helper function.\n",
"function error_exit\n",
"{\n",
" /opt/aws/bin/cfn-signal -e 1 -r \"$1\" '",
{
"Ref": "WaitHandle"
},
"'\n",
" exit 1\n",
"}\n",
"# Install the AWS CodeDeploy Agent.\n",
"cd /home/ec2-user/\n",
"aws s3 cp 's3://aws-codedeploy-us-east-1/latest/codedeploy-agent.noarch.rpm' . || error_exit 'Failed to download AWS CodeDeploy Agent.'\n",
"yum -y install codedeploy-agent.noarch.rpm || error_exit 'Failed to install AWS CodeDeploy Agent.' \n",
"/opt/aws/bin/cfn-init -s ",
{
"Ref": "AWS::StackId"
},
" -r LinuxEC2Instance --region ",
{
"Ref": "AWS::Region"
},
" || error_exit 'Failed to run cfn-init.'\n",
"# All is well, so signal success.\n",
"/opt/aws/bin/cfn-signal -e 0 -r \"AWS CodeDeploy Agent setup complete.\" '",
{
"Ref": "WaitHandle"
},
"'\n"
]
]
}
},
"KeyName": {
"Ref": "KeyPairName"
},
"Tags": [
{
"Key": {
"Ref": "TagKey"
},
"Value": {
"Ref": "TagValue"
}
}
],
"IamInstanceProfile": {
"Ref": "InstanceRoleInstanceProfile"
}
}
},
"WindowsEC2Instance5": {
"Type": "AWS::EC2::Instance",
"Condition": "LaunchWindowsEC2Instance5",
"Metadata": {
"AWS::CloudFormation::Init": {
"config": {
"commands": {
"00-download-host-agent": {
"command": {
"Fn::Join": [
"",
[
"powershell.exe -Command \"Read-S3Object ",
"-BucketName aws-codedeploy-us-east-1 ",
"-Key latest/codedeploy-agent.msi ",
"-File codedeploy-agent.msi\""
]
]
},
"cwd": "C:/cfn",
"waitAfterCompletion": 0
},
"01-install-host-agent": {
"command": "C:\\cfn\\codedeploy-agent.msi /quiet /l C:\\cfn\\host-agent-install-log.txt",
"ignoreErrors": "true",
"waitAfterCompletion": 0
},
"02-signal-ready": {
"command": {
"Fn::Join": [
"",
[
"\"C:\\Program Files\\Amazon\\cfn-bootstrap\\cfn-signal\"",
" -e 0 \"",
{
"Ref": "WaitHandle"
},
"\""
]
]
},
"waitAfterCompletion": 0
}
},
"services": {
"windows": {
"codedeploy-agent": {
"enabled": "true",
"ensureRunning": "true",
"commands": [
"01-install-host-agent"
]
}
}
}
}
}
},
"Properties": {
"ImageId": {
"Fn::FindInMap": [
"RegionOS2AMI",
{
"Ref": "AWS::Region"
},
{
"Ref": "OperatingSystem"
}
]
},
"InstanceType": {
"Ref": "InstanceType"
},
"SecurityGroups": [
{
"Ref": "SecurityGroup"
}
],
"UserData": {
"Fn::Base64": {
"Fn::Join": [
"",
[
"<script>\n",
"mkdir c:\\cfn\n",
"mkdir c:\\cfn\\log\n",
"c:\\\"Program Files\"\\Amazon\\cfn-bootstrap\\cfn-init.exe -s ",
{
"Ref": "AWS::StackName"
},
" --region ",
{
"Ref": "AWS::Region"
},
" -r WindowsEC2Instance > c:\\cfn\\log\\cfn-call-log 2>&1",
"</script>"
]
]
}
},
"KeyName": {
"Ref": "KeyPairName"
},
"Tags": [
{
"Key": {
"Ref": "TagKey"
},
"Value": {
"Ref": "TagValue"
}
}
],
"IamInstanceProfile": {
"Ref": "InstanceRoleInstanceProfile"
}
}
}
},
"Outputs": {
"CodeDeployTrustRoleARN": {
"Value": {
"Fn::GetAtt": [
"CodeDeployTrustRole",
"Arn"
]
}
},
"PublicIp": {
"Value": {
"Fn::GetAtt": [
"LinuxEC2Instance",
"PublicIp"
]
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment