Welcome to CloudAffaire and this is Debjeet.
In the last blog post, we have created a CloudFormation template using YAML.
In this blog post, we are going to create a CloudFormation stack using designers. The stack will contain VPC, subnet and one EC2 instance.
Step 1: Login to AWS console and navigate to ‘CloudFormation’.
Note: For shortening of the code, only has AMI mapping for APAC regions. Make sure you are in any one of the APAC region.
Step 2: Click ‘Create Stack’.
Step 3: Select ‘Create template in Designer’ and click ‘Create template in designer’.
Step 4: Change the name of your template and select YAML as the template language.
Step 5: In the ‘Parameters’ tab paste below YAML code.
Parameters: InstanceType: Description: WebServer EC2 instance type Type: String Default: t2.small AllowedValues: - t1.micro - t2.nano - t2.micro - t2.small - t2.medium ConstraintDescription: must be a valid EC2 instance type. KeyName: Description: Name of an EC2 KeyPair to enable SSH access to the instance. Type: 'AWS::EC2::KeyPair::KeyName' ConstraintDescription: must be the name of an existing EC2 KeyPair.
Make sure indentation is correct, you can also use cloudformation_designer_demo.yaml from below GitHub repo.
Note: Every time you make any changes to the template in the designer, it will ask for a refresh. Click on the refresh icon to refresh the designer.
Step 6: Click on ‘Mapping’ tab under ‘Components’ and paste below code.
Mappings: AWSInstanceType2Arch: t1.micro: Arch: HVM64 t2.nano: Arch: HVM64 t2.micro: Arch: HVM64 t2.small: Arch: HVM64 t2.medium: Arch: HVM64 AWSRegionArch2AMI: ap-northeast-1: HVM64: ami-06cd52961ce9f0d85 ap-northeast-2: HVM64: ami-0a10b2721688ce9d2 ap-northeast-3: HVM64: ami-0d98120a9fb693f07 ap-southeast-1: HVM64: ami-08569b978cc4dfa10 ap-southeast-2: HVM64: ami-09b42976632b27e9b ap-south-1: HVM64: ami-0912f71e06545ad88
Note: You can define other template elements as well by selecting the respective element tabs. Next, we will add resources to our template.
Make sure indentation is correct in above code, you can also use cloudformation_designer_demo.yaml from below GitHub repo.
Step 7: Expand ‘EC2’ resource type and drag ‘VPC’ to designer and in the ‘Properties’ tab, edit the name of the resource to ‘myVPC’.
Next, we are going to create the subnet which will host the EC2 instance.
Step 8: Drag ‘Subnet’ under ‘myVPC’ in designer and in the ‘Properties’ tab, edit the name of the resource to ‘mySUBNET’.
Observe: Designer has automatically assigned VPC id reference of ‘myVPC’ to ‘mySUBNET’ VpcId property. Hence ‘mySUBNET’ will be created under ‘myVPC’.
Step 9: Drag ‘Instance’ under ‘mySUBNET’ in designer and in the ‘Properties’ tab, edit the name of the resource to ‘myINSTANCE’.
We have previously defined parameters and mapping for our template. To make the resources use those, we need to manually update resource section of the template code.
Step 10: Update the resource properties under ‘Template’.
CidrBlock: 10.0.0.0/16 Tags: - Key: "Name" Value: "myVPC"
VpcId: !Ref myVPC CidrBlock: 10.0.0.0/24 Tags: - Key: "Name" Value: "mySUBNET"
InstanceType: !Ref InstanceType ImageId: !FindInMap - AWSRegionArch2AMI - !Ref 'AWS::Region' - !FindInMap - AWSInstanceType2Arch - !Ref InstanceType - Arch KeyName: !Ref KeyName SubnetId: !Ref mySUBNET Tags: - Key: "Name" Value: "myInstance"
Note: Make sure your code indentation is correct or you will get an error. You can also use cloudformation_designer_demo.yaml from below GitHub repo.
Our template is ready for deployment. Next, we are going to create a stack using this template.
Step 11: Click on the cloud icon to upload this template in S3 and create a stack.
You will be redirected to stack creation window.
Step 12: Provide the parameters for your stack and create the stack.
Step 13: Cleanup.
Note: This will also delete the underlying resources for this stack.
Hope you have enjoyed this article. In the next blog post, we will discuss intrinsic functions in CloudFormation.
To get more details on CloudFormation, please refer below AWS documentation