diff --git a/14-InfrastructureAsCode (CloudFormation)/CFNINIT_CFNHUP_CFNSIGNAL/1_userdata.json b/14-InfrastructureAsCode (CloudFormation)/CFNINIT_CFNHUP_CFNSIGNAL/1_userdata.json index 0cbe9a2..b8f10c7 100644 --- a/14-InfrastructureAsCode (CloudFormation)/CFNINIT_CFNHUP_CFNSIGNAL/1_userdata.json +++ b/14-InfrastructureAsCode (CloudFormation)/CFNINIT_CFNHUP_CFNSIGNAL/1_userdata.json @@ -1,21 +1,66 @@ { -"Parameters": { - "LatestAmiId": { - "Type" : "AWS::SSM::Parameter::Value", - "Description" : "AMI for EC2", - "Default": "/aws/service/ami-amazon-linux-latest/amzn2-ami-hvm-x86_64-gp2" - } -}, -"Resources": { - "Bucket": { - "Type": "AWS::S3::Bucket" - }, - "Instance": { - "Type": "AWS::EC2::Instance", - "Properties": { - "InstanceType": "t2.micro", - "ImageId": { "Ref" : "LatestAmiId" } - } - } -} + "Parameters": { + "LatestAmiId": { + "Description": "AMI for EC2", + "Type": "AWS::SSM::Parameter::Value", + "Default": "/aws/service/ami-amazon-linux-latest/amzn2-ami-hvm-x86_64-gp2" + }, + "Message": { + "Description": "Message for HTML page", + "Default": "Cats are the best", + "Type": "String" + } + }, + "Resources": { + "InstanceSecurityGroup": { + "Type": "AWS::EC2::SecurityGroup", + "Properties": { + "GroupDescription": "Enable SSH and HTTP access via port 22 IPv4 & port 80 IPv4", + "SecurityGroupIngress": [ + { + "Description": "Allow SSH IPv4 IN", + "IpProtocol": "tcp", + "FromPort": "22", + "ToPort": "22", + "CidrIp": "0.0.0.0/0" + }, + { + "Description": "Allow HTTP IPv4 IN", + "IpProtocol": "tcp", + "FromPort": "80", + "ToPort": "80", + "CidrIp": "0.0.0.0/0" + } + ] + } + }, + "Bucket": { + "Type": "AWS::S3::Bucket" + }, + "Instance": { + "Type": "AWS::EC2::Instance", + "Properties": { + "InstanceType": "t2.micro", + "ImageId": { + "Ref": "LatestAmiId" + }, + "SecurityGroupIds": [ + { + "Ref": "InstanceSecurityGroup" + } + ], + "Tags": [ + { + "Key": "Name", + "Value": "A4L-UserData Test" + } + ], + "UserData": { + "Fn::Base64": { + "Fn::Sub": "#!/bin/bash -xe\nyum -y update\nyum -y upgrade\n# simulate some other processes here\nsleep 300\n# Continue\nyum install -y httpd\nsystemctl enable httpd\nsystemctl start httpd\necho \"Amazing test page

${Message}

\" > /var/www/html/index.html\n" + } + } + } + } + } } \ No newline at end of file diff --git a/14-InfrastructureAsCode (CloudFormation)/CFNINIT_CFNHUP_CFNSIGNAL/2_userdata with signal.json b/14-InfrastructureAsCode (CloudFormation)/CFNINIT_CFNHUP_CFNSIGNAL/2_userdata with signal.json new file mode 100644 index 0000000..604577e --- /dev/null +++ b/14-InfrastructureAsCode (CloudFormation)/CFNINIT_CFNHUP_CFNSIGNAL/2_userdata with signal.json @@ -0,0 +1,71 @@ +{ + "Parameters": { + "LatestAmiId": { + "Description": "AMI for EC2", + "Type": "AWS::SSM::Parameter::Value", + "Default": "/aws/service/ami-amazon-linux-latest/amzn2-ami-hvm-x86_64-gp2" + }, + "Message": { + "Description": "Message for HTML page", + "Default": "Cats are the best", + "Type": "String" + } + }, + "Resources": { + "InstanceSecurityGroup": { + "Type": "AWS::EC2::SecurityGroup", + "Properties": { + "GroupDescription": "Enable SSH and HTTP access via port 22 IPv4 & port 80 IPv4", + "SecurityGroupIngress": [ + { + "Description": "Allow SSH IPv4 IN", + "IpProtocol": "tcp", + "FromPort": "22", + "ToPort": "22", + "CidrIp": "0.0.0.0/0" + }, + { + "Description": "Allow HTTP IPv4 IN", + "IpProtocol": "tcp", + "FromPort": "80", + "ToPort": "80", + "CidrIp": "0.0.0.0/0" + } + ] + } + }, + "Bucket": { + "Type": "AWS::S3::Bucket" + }, + "Instance": { + "Type": "AWS::EC2::Instance", + "CreationPolicy": { + "ResourceSignal": { + "Timeout": "PT15M" + } + }, + "Properties": { + "InstanceType": "t2.micro", + "ImageId": { + "Ref": "LatestAmiId" + }, + "SecurityGroupIds": [ + { + "Ref": "InstanceSecurityGroup" + } + ], + "Tags": [ + { + "Key": "Name", + "Value": "A4L-UserData Test" + } + ], + "UserData": { + "Fn::Base64": { + "Fn::Sub": "#!/bin/bash -xe\nyum -y update\nyum -y upgrade\n# simulate some other processes here\nsleep 300\n# Continue\nyum install -y httpd\nsystemctl enable httpd\nsystemctl start httpd\necho \"Amazing test page

${Message}

\" > /var/www/html/index.html\n/opt/aws/bin/cfn-signal -e $? --stack ${AWS::StackId} --resource Instance --region ${AWS::Region}\n" + } + } + } + } + } +} \ No newline at end of file diff --git a/14-InfrastructureAsCode (CloudFormation)/CFNINIT_CFNHUP_CFNSIGNAL/3_cfninit with signal.json b/14-InfrastructureAsCode (CloudFormation)/CFNINIT_CFNHUP_CFNSIGNAL/3_cfninit with signal.json new file mode 100644 index 0000000..b1b303e --- /dev/null +++ b/14-InfrastructureAsCode (CloudFormation)/CFNINIT_CFNHUP_CFNSIGNAL/3_cfninit with signal.json @@ -0,0 +1,105 @@ +{ + "Parameters": { + "LatestAmiId": { + "Description": "AMI for EC2", + "Type": "AWS::SSM::Parameter::Value", + "Default": "/aws/service/ami-amazon-linux-latest/amzn2-ami-hvm-x86_64-gp2" + }, + "Message": { + "Description": "Message for HTML page", + "Default": "Cats are the best", + "Type": "String" + } + }, + "Resources": { + "InstanceSecurityGroup": { + "Type": "AWS::EC2::SecurityGroup", + "Properties": { + "GroupDescription": "Enable SSH and HTTP access via port 22 IPv4 & port 80 IPv4", + "SecurityGroupIngress": [ + { + "Description": "Allow SSH IPv4 IN", + "IpProtocol": "tcp", + "FromPort": "22", + "ToPort": "22", + "CidrIp": "0.0.0.0/0" + }, + { + "Description": "Allow HTTP IPv4 IN", + "IpProtocol": "tcp", + "FromPort": "80", + "ToPort": "80", + "CidrIp": "0.0.0.0/0" + } + ] + } + }, + "Bucket": { + "Type": "AWS::S3::Bucket" + }, + "Instance": { + "Type": "AWS::EC2::Instance", + "Metadata": { + "AWS::CloudFormation::Init": { + "config": { + "packages": { + "yum": { + "httpd": [] + } + }, + "files": { + "/var/www/html/index.html": { + "content": { + "Fn::Sub": "Amazing test page

${Message}

\n" + } + } + }, + "commands": { + "simulatebootstrap": { + "command": "sleep 300" + } + }, + "services": { + "sysvinit": { + "httpd": { + "enabled": "true", + "ensureRunning": "true", + "files": [ + "/var/www/html/index.html" + ] + } + } + } + } + } + }, + "CreationPolicy": { + "ResourceSignal": { + "Timeout": "PT15M" + } + }, + "Properties": { + "InstanceType": "t2.micro", + "ImageId": { + "Ref": "LatestAmiId" + }, + "SecurityGroupIds": [ + { + "Ref": "InstanceSecurityGroup" + } + ], + "Tags": [ + { + "Key": "Name", + "Value": "A4L-UserData Test" + } + ], + "UserData": { + "Fn::Base64": { + "Fn::Sub": "#!/bin/bash -xe\n/opt/aws/bin/cfn-init -v --stack ${AWS::StackId} --resource Instance --region ${AWS::Region}\n/opt/aws/bin/cfn-signal -e $? --stack ${AWS::StackId} --resource Instance --region ${AWS::Region}\n" + } + } + } + } + } +} \ No newline at end of file diff --git a/14-InfrastructureAsCode (CloudFormation)/CFNINIT_CFNHUP_CFNSIGNAL/4_cfninit with signal and cfnhup.json b/14-InfrastructureAsCode (CloudFormation)/CFNINIT_CFNHUP_CFNSIGNAL/4_cfninit with signal and cfnhup.json new file mode 100644 index 0000000..95e85e6 --- /dev/null +++ b/14-InfrastructureAsCode (CloudFormation)/CFNINIT_CFNHUP_CFNSIGNAL/4_cfninit with signal and cfnhup.json @@ -0,0 +1,129 @@ +{ + "Parameters": { + "LatestAmiId": { + "Description": "AMI for EC2", + "Type": "AWS::SSM::Parameter::Value", + "Default": "/aws/service/ami-amazon-linux-latest/amzn2-ami-hvm-x86_64-gp2" + }, + "Message": { + "Description": "Message for HTML page", + "Default": "Cats are the best", + "Type": "String" + } + }, + "Resources": { + "InstanceSecurityGroup": { + "Type": "AWS::EC2::SecurityGroup", + "Properties": { + "GroupDescription": "Enable SSH and HTTP access via port 22 IPv4 & port 80 IPv4", + "SecurityGroupIngress": [ + { + "Description": "Allow SSH IPv4 IN", + "IpProtocol": "tcp", + "FromPort": "22", + "ToPort": "22", + "CidrIp": "0.0.0.0/0" + }, + { + "Description": "Allow HTTP IPv4 IN", + "IpProtocol": "tcp", + "FromPort": "80", + "ToPort": "80", + "CidrIp": "0.0.0.0/0" + } + ] + } + }, + "Bucket": { + "Type": "AWS::S3::Bucket" + }, + "Instance": { + "Type": "AWS::EC2::Instance", + "Metadata": { + "AWS::CloudFormation::Init": { + "config": { + "packages": { + "yum": { + "httpd": [] + } + }, + "files": { + "/etc/cfn/cfn-hup.conf": { + "content": { + "Fn::Sub": "[main]\nstack=${AWS::StackName}\nregion=${AWS::Region}\ninterval=1\nverbose=true\n" + }, + "mode": "000400", + "owner": "root", + "group": "root" + }, + "/etc/cfn/hooks.d/cfn-auto-reloader.conf": { + "content": { + "Fn::Sub": "[cfn-auto-reloader-hook]\ntriggers=post.update\npath=Resources.Instance.Metadata.AWS::CloudFormation::Init\naction=/opt/aws/bin/cfn-init -v --stack ${AWS::StackId} --resource Instance --region ${AWS::Region}\nrunas=root\n" + }, + "mode": "000400", + "owner": "root", + "group": "root" + }, + "/var/www/html/index.html": { + "content": { + "Fn::Sub": "Amazing test page

${Message}

\n" + } + } + }, + "commands": { + "simulatebootstrap": { + "command": "sleep 300" + } + }, + "services": { + "sysvinit": { + "cfn-hup": { + "enabled": "true", + "ensureRunning": "true", + "files": [ + "/etc/cfn/cfn-hup.conf", + "/etc/cfn/hooks.d/cfn-auto-reloader.conf" + ] + }, + "httpd": { + "enabled": "true", + "ensureRunning": "true", + "files": [ + "/var/www/html/index.html" + ] + } + } + } + } + } + }, + "CreationPolicy": { + "ResourceSignal": { + "Timeout": "PT15M" + } + }, + "Properties": { + "InstanceType": "t2.micro", + "ImageId": { + "Ref": "LatestAmiId" + }, + "SecurityGroupIds": [ + { + "Ref": "InstanceSecurityGroup" + } + ], + "Tags": [ + { + "Key": "Name", + "Value": "A4L-UserData Test" + } + ], + "UserData": { + "Fn::Base64": { + "Fn::Sub": "#!/bin/bash -xe\n/opt/aws/bin/cfn-init -v --stack ${AWS::StackId} --resource Instance --region ${AWS::Region}\n/opt/aws/bin/cfn-signal -e $? --stack ${AWS::StackId} --resource Instance --region ${AWS::Region}\n" + } + } + } + } + } +} \ No newline at end of file