AWS CloudFormation Template-AWS VPC with a single subnet, a security group, and an Amazon EC2

Introduction :-

AWS CloudFormation template is a fundamental blueprint for deploying a network infrastructure on #Amazon Web Services (AWS). CloudFormation is a powerful #infrastructure as code (IAC) service that enables you to define and provision AWS resources in a declarative manner. This template sets up a #Virtual Private Cloud (VPC) with a subnet, a security group, and an EC2 instance, forming the building blocks of a basic AWS environment.

The provided #AWS CloudFormation template defines a set of AWS resources to create a basic #AWS Virtual Private Cloud (VPC) with a single subnet, a security group, and an #Amazon Elastic Compute Cloud (EC2) instance.

Here’s a breakdown of the template:

  1. AWSTemplateFormatVersion: Specifies the CloudFormation template version.

  2. Resources: This section defines the AWS resources that will be created when you deploy the template.

  3. a. VPC: Defines an AWS VPC (Virtual Private Cloud) with a specified CIDR block (10.0.0.0/16).

  4. b. Subnet: Defines an AWS subnet within the VPC with a specified CIDR block (10.0.1.0/24) and associates it with the previously defined VPC using the !Ref function.

  5. c. SecurityGroup: Defines an AWS security group, which is used to control inbound traffic to the associated EC2 instances. In this case, it allows incoming SSH traffic (TCP port 22) from any IP address (0.0.0.0/0). The security group is associated with the VPC created earlier using the !Ref function.

  6. d. EC2Instance: Defines an AWS EC2 instance. It specifies the instance type (t2.micro), the Amazon Machine Image (AMI) to use, a key pair (KeyName) for SSH access, and the subnet and security group to associate with this EC2 instance. The !Ref function is used to reference the Subnet and SecurityGroup resources defined earlier.

This template creates a basic networking environment, allowing you to launch an EC2 instance in the specified subnet with SSH access via the defined security group. Please note that the KeyName “Test” should correspond to an existing EC2 key pair in your AWS account to enable SSH access to the instance. Also, ensure that the specified AMI (ami-01a4f99c4ac11b03c) is available in your AWS region.

You can deploy this CloudFormation template using the AWS CloudFormation service to create the described resources.

Deployment Steps

Follow these steps to upload and create the CloudFormation stack using the #AWS Management Console:

  1. Sign in to the AWS Management Console: Log in to your AWS account if you haven’t already.

2. Navigate to CloudFormation: Go to the #AWS CloudFormation service from the AWS Management Console.

3. Click the “Create stack” button.

4. Upload the CloudFormation template file (YAML).

Yaml :

AWSTemplateFormatVersion: '2010-09-09'
Resources:
  VPC:
    Type: AWS::EC2::VPC
    Properties:
      CidrBlock: 10.0.0.0/16

  Subnet:
    Type: AWS::EC2::Subnet
    Properties:
      VpcId: !Ref VPC
      CidrBlock: 10.0.1.0/24

  SecurityGroup:
    Type: AWS::EC2::SecurityGroup
    Properties:
      GroupDescription: Allow SSH access
      VpcId: !Ref VPC
      SecurityGroupIngress:
        - IpProtocol: tcp
          FromPort: 22
          ToPort: 22
          CidrIp: 0.0.0.0/0

  EC2Instance:
    Type: AWS::EC2::Instance
    Properties:
      InstanceType: t2.micro
      ImageId: ami-01a4f99c4ac11b03c
      KeyName: Test
      SubnetId: !Ref Subnet
      SecurityGroupIds:
        - !Ref SecurityGroup

5. Specify Stack Details:

  • Enter a Stack name for your deployment.

  • Provide parameter values as needed.

  • Review and acknowledge the capabilities .

  • You can set additional stack options or tags if necessary.

6. Review and Create:

  • Review the stack details and configuration.

  • Click “Create stack” to initiate the deployment.

7. Monitor Stack Creation:

  • The CloudFormation stack creation process will begin.

  • Monitor the stack events in the AWS Management Console.

Conclusion:

CloudFormation template is just the beginning of what you can achieve with #AWS infrastructure as code. By extending and customizing this template, you can create complex, multi-tiered architectures, manage dependencies, and ensure that your infrastructure is both reproducible and version-controlled. #AWS CloudFormation simplifies resource management, making it easier to maintain and scale your #AWS infrastructure, ultimately streamlining the deployment and management of your applications. It’s a fundamental tool for automating and orchestrating AWS resources, enhancing your ability to efficiently and consistently deploy cloud-based solutions.

0
Subscribe to my newsletter

Read articles from Mahira Technology Private Limited directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Mahira Technology Private Limited
Mahira Technology Private Limited

A leading tech consulting firm specializing in innovative solutions. Experts in cloud, DevOps, automation, data analytics & more. Trusted technology partner.