Create CloudFormation Template
Hello Everyone
Welcome to CloudAffaire and this is Debjeet.
In the last blog post, we have created a CloudFormation stack using a sample template provided by AWS.
https://cloudaffaire.com/create-cloudformation-stack/
In this blog post, we are going to create our 1st CloudFormation template and using this template we will create a CloudFormation stack in AWS console. We have tried to include most of the template elements in this demo.
Create CloudFormation Template:
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 regions.
Step 2: Click ‘Create Stack’.
Step 3: Open your favorite editor and create a file with .yaml extension using below YAML code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
## Template to create EC2 instance based on user input, below template elements are used in this ## AWSTemplateFormatVersion: The AWS CloudFormation template version, currently only supports 2010-09-09 ## Description: A text string that describes the template. ## Metadata: A text string that describes additional information about the template ## Mappings: A mapping of keys and associated values that you can use to specify conditional parameter values. ## Parameters: Values to pass to your template at runtime. ## Conditions: Controls resource creation\property assignment or outputs. ## Resources: Specifies the stack resources and their properties. ## Outputs: Values to be returned. #provide template version, description and metadata AWSTemplateFormatVersion: "2010-09-09" Description: 'Create an EC2 instance' Metadata: VPC: Description: "Change the instance tag, elastic ip assignment based on user input" #map ami-id according to the region you are in (for shortening of the code, only defined APAC regions) Mappings: RegionMap: ap-south-1: AMI: "ami-0889b8a448de4fc44" ap-southeast-1: AMI: "ami-0b419c3a4b01d1859" ap-northeast-2: AMI: "ami-047f7b46bd6dd5d84" ap-northeast-1: AMI: "ami-0f9ae750e8274075b" ap-southeast-2: AMI: "ami-04481c741a0311bbb" #take instance type from user Parameters: InstanceType: Description: Instance Type. Default: web Type: String AllowedValues: [chache, web, db] ConstraintDescription: must specify web or db. Default is web #define condition according to user input Conditions: DatabaseServer: !Equals [!Ref InstanceType, "db"] WebServer: !Equals [!Ref InstanceType, "web"] CacheServer: !Equals [!Ref InstanceType, "chache"] #define resources that will be created Resources: EC2Instance: Type: "AWS::EC2::Instance" Properties: ImageId: !FindInMap [RegionMap, !Ref "AWS::Region", AMI] #get the ami-id from mapping based on region (APAC) InstanceType: "t2.micro" KeyName: "debjeet-KP" Tags: - Key: "Name" Value: !If [WebServer, "webserver", !If [CacheServer, "cacheserver", "dbserver"]] #give a tag according to condition ElasticIP: Type: AWS::EC2::EIP Condition: DatabaseServer #create elastic ip only for db instance Properties: Domain: "vpc" ElasticIPAssignment: Type: AWS::EC2::EIPAssociation Condition: DatabaseServer #attach the elastic ip to the instance only for db instance Properties: EIP: !Ref ElasticIP InstanceId: !Ref EC2Instance #define outputs that will be displayed Outputs: instanceID: Description: EC2 instance id Value: !Ref EC2Instance instanceDescription: Description: EC2 instance type Value: !If [WebServer, "webserver", !If [CacheServer, "cacheserver", "dbserver"]] elasticIP: Condition: DatabaseServer #print the elastic ip if created Description: Elastic IP Address Value: !Ref ElasticIP |
Note: Make sure code indentation id correct or you can download the template_elements_demo.yaml file from below GitHub repo.
https://github.com/CloudAffaire/CloudFormation
Step 4: Select ‘Template is ready’ and then ‘Upload a template file’, click ‘Choose file’ to upload the file and click ‘Next’.
Step 5: Provide name, parameters for your stack and click ‘Next’.
Step 6: Leave ‘Configure stack options’ as it is and click ‘Next’.
Step 7: Review your stack configuration and click ‘Create stack’.
Note: It will take some time to complete the stack creation. You can monitor the progress in ‘Events’ section.
Our stack successfully created using the template created in step 3.
Correlate the code with below tabs output for your understanding.
Events: Stack creation details with the timeline (used for troubleshooting if stack creation failed)
Resource: AWS resources created under this stack (in our case one EC2 instance, if db then elastic IP)
Outputs: Values that are returned (In our instance id, type and if db then elastic IP address)
Parameters: Values passed to your template at runtime (cache or web or db)
Template: The actual code for this stack (defined in step 3)
Step 8: 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 create a template using CloudFormation Designer.
To get more details on CloudFormation, please refer below AWS documentation
https://docs.aws.amazon.com/cloudformation/index.html
Hello, how would I like to see if you could help me? Will my code be good? because it gives me some errors
AWSTemplateFormatVersion: 2010-09-09
Parameters:
EC2Instance:
Type: ‘AWS::EC2::Image::Id’
Default: ami-01e24be29428c15b2
SubnetID:
Type:’AWS::EC2::Subnet::Id’
Description:Subnet de la instancia
SecurityGroupsIDs:a
Type: ‘AWS::EC2::SecurityGroup::Id’
Default: sg-0d68df229cf090306
Description:grupo de seguridad
KeyName:
Type: ‘AWS::EC2::KeyPair::KeyName’
Default: xxxxxxx
Description: nombre key
InstanceType:
Type:String
Default: t2.micro
Description:EC2 tipo instancia
Storage:
Default: 8
Type: String
Description: gb
Resources:
EC2Instance:
Type:Type: AWS::EC2::Image::Id
Properties:
ImageId: !Ref EC2Instance
KeyName: !Ref KeyName
SecurityGroupIds: !Ref SecurityGroupsIDs
SubnetId: !Ref SubnetID
Tags:
-Key:Owner
Value: prueba
-Origin:
Value: DevOps Academy 2019