Learn to use AWS CloudFormation with a YAML template to deploy an EC2 instance, including defining resources, parameters, and outputs.
In this lesson, you’ll learn how to work with AWS CloudFormation by using a YAML template to deploy an EC2 instance. While CloudFormation supports both YAML and JSON, this demo focuses on YAML for its readability and simplicity.We’ll walk through creating a file named stack.yaml that configures our CloudFormation stack. This template includes sections for defining resources such as EC2 instances and security groups, along with parameters and outputs to customize and retrieve important deployment details.
To start, we define an EC2 instance as a resource. First, we assign the resource a logical name (“Ec2Instance”) and specify its type as AWS::EC2::Instance, as outlined in the AWS documentation.
Copy
Ask AI
Resources: Ec2Instance:
Next, add the resource’s Type and Properties. CloudFormation uses the Type field to identify the resource, and the Properties section allows you to specify configuration details for your instance such as security groups, tags, and AMI details. The example below highlights a standard configuration excerpt:
To enhance your deployment, you can add a security group resource that controls access to your instance. Begin by defining the security group with a logical name (“InstanceSecurityGroup”), its type (AWS::EC2::SecurityGroup), and a description.
Copy
Ask AI
Resources: Ec2Instance: Type: AWS::EC2::Instance Properties: ImageId: ami-041feb57c611358bd KeyName: main Tags: - Key: Name Value: myEc2Instance InstanceSecurityGroup: Type: AWS::EC2::SecurityGroup Properties: GroupDescription: Enable SSH access via port 22
Then, specify the ingress rules for this security group. In the example below, TCP port 22 is opened for SSH access from any IP address:
Copy
Ask AI
Properties: ImageId: ami-041feb57c611358bd KeyName: main Tags: - Key: Name Value: myEc2InstanceInstanceSecurityGroup: Type: AWS::EC2::SecurityGroup Properties: GroupDescription: Enable SSH access via port 22 SecurityGroupIngress: - IpProtocol: tcp FromPort: 22 ToPort: 22 CidrIp: 0.0.0.0/0
If you intend to allow access over a range of ports, modify FromPort and ToPort accordingly. For a single port, both values remain the same.
Finally, integrate the security group reference into the EC2 instance. CloudFormation enables you to refer to other resources using the !Ref intrinsic function:
Copy
Ask AI
Resources: Ec2Instance: Type: AWS::EC2::Instance Properties: SecurityGroups: - !Ref InstanceSecurityGroup ImageId: ami-041feb57c611358bd KeyName: main Tags: - Key: Name Value: myEc2Instance InstanceSecurityGroup: Type: AWS::EC2::SecurityGroup Properties: GroupDescription: Enable SSH access via port 22 SecurityGroupIngress: - IpProtocol: tcp FromPort: 22 ToPort: 22 CidrIp: 0.0.0.0/0
To provide flexibility during deployment, you can introduce parameters for customizable values such as the EC2 instance name and key pair. The example below creates parameters for the key pair and instance name. By setting the type of KeyName to AWS::EC2::KeyPair::KeyName, CloudFormation displays a dropdown list of available key pairs.
Outputs enable you to extract and display key information after the stack is deployed. In this template, the public IP address of the EC2 instance is output using the !GetAtt function to access the PublicIp attribute.
Copy
Ask AI
Outputs: PublicIp: Description: Server Public IP Value: !GetAtt Ec2Instance.PublicIp
Click Create stack and choose to upload your template file.
Select the stack.yaml file.
Enter a stack name (e.g., “my-deployment”).
Specify the parameters:
For the EC2 instance name, enter a desired value (for example, “this is the server”).
For the key pair, select the appropriate key from the dropdown.
Click Next to configure additional options such as tags, rollback settings, or notifications.
Review and submit the stack for deployment.
The image below illustrates the AWS CloudFormation console during stack creation:
After deployment, the stack status will change to “CREATE_COMPLETE”. You can review the events and check resource details, including obtaining the EC2 instance’s public IP address from the Outputs tab.The following image shows the stack deployment events in the CloudFormation console:
If you need to make changes, update the stack using the template designer within the AWS CloudFormation console. To delete a stack, simply select it in the console and click Delete. This action removes all resources created by the stack.The image below shows the update interface in the AWS CloudFormation console:
That concludes this lesson on AWS CloudFormation. Enjoy automating your infrastructure and check back for more detailed tutorials in our upcoming lessons!