Dheeraj Choudhary
Dheeraj Choudhary's Blog

Dheeraj Choudhary's Blog

Deploy AWS Cloudformation Template Using AWS CLI | Create Virtual Private Cloud , Internet Gateway & Associate

Deploy AWS Cloudformation Template Using AWS CLI | Create Virtual Private Cloud , Internet Gateway & Associate

Dheeraj Choudhary
·May 1, 2022·

6 min read

Subscribe to my newsletter and never miss my upcoming articles

Play this article

Table of contents

Welcome to the series of AWS Cloudformation For Beginners 👨🏻‍💻. In this entire series we will focus on our core concepts of Cloudformation by launching important basic services from scratch which will take your infra-as-code journey to next level. This series would start from beginner to advance with real life Usecases and Youtube Tutorials.

If you are a beginner and want to start your journey towards infra-as-code developer as part of your devops role buckle up 🚴‍♂️ and lets get started and understand core cloudformation concepts by implementing it...🎬

❗️❗️Pre-Requisite❗️❗️

1️⃣ Add VS Code Cloudformation extension

2️⃣ Adding VS Code Indentation Extension For Cloudformation Templates [Optional]

🌟Launch Virtual Private Cloud[VPC]🌟

Create custom VPC with custom CIDR Block and enable dnshostname, dnssupport. As part of this template we will use below components

🔳 Resources
CustomVPC :- Using this resource you can specify a VPC with its specified IPv4 CIDR block. Remember the smallest VPC allowed uses a /28 netmask (16 IPv4 addresses), and largest VPC allowed uses a /16 netmask (65,536 IPv4 addresses).
🔳 Resource Properties
CIDR :- Its full form is "Classless Inter-Domain Routing (CIDR)" and basically its methodolgy for allocating IP addresses and IP routing. In AWS you can have primary CIDR block and secondry CIDR block option also.
EnableDnsSupport :- This resource property signifies that DNS resolution is supported for the VPC. If this is enabled, query to the AWS provided DNS server at the 169.254.169.253 IP, or the reserved IP of the VPC network range "plus two" succeed. If its disabled, the AWS provided DNS service in the VPC that resolves public DNS hostnames to IP addresses is not enabled. Enabled by default.
EnableDnsHostnames :- This resource property if enabled signifies if instances launched in the VPC get DNS hostnames. Disabled wont have DNS hostnames .
Tags:- Tags to set naming based on the tagging policy defined.
🔳 Outputs: Its always a best practice to print output for your resources.
outputVPC: A reference to the created VPC.

Resources:
  CustomVPC:
    Type: AWS::EC2::VPC
    Properties:
      CidrBlock: 10.0.0.0/24
      EnableDnsHostnames: true
      EnableDnsSupport: true
      Tags:
        - Key: Name
          Value: CutomVPC
Outputs:
  outputVPC:
    Description: A reference to the created VPC
    Value: !Ref CustomVPC

🔊To view entire github code click here

1️⃣ Lets validate our template 👨‍💻

aws cloudformation validate-template --template-body file://vpc.yaml

2️⃣ After successfull template verification lets create stack using our template aws cloudformation 👨‍💻

create-stack --stack-name onlyvpc --template-body file://vpc.yaml

3️⃣ Check if the stack we created via template is completed successfully 👨‍💻

aws cloudformation list-stack-resources --stack-name onlyvpc

4️⃣ Describe stack to view its properties 👨‍💻

aws cloudformation describe-stacks --stack-name onlyvpc

5️⃣ Check events for stack formation 👨‍💻

aws cloudformation describe-stack-events --stack-name onlyvpc

👁‍🗨👁‍🗨 YouTube Tutorial 📽

🌟Launch Internet Gateway & Associate🌟

Here we will launch new InternetGateway resource and attach it to VPC so that it can communicate via Internet.

🔳 Parameters:-
CustomVPC :- Using this parameter for VPC "AWS::EC2::VPC::Id" we can list existing VPC list into the account and select anyone from them. Apart from this list we can also you default value if no value is selected in the parameter.
CustomInternetGateway :- Using this parameter for Internet gateway with type "String" we are setting default value for this parameter.
🔳 Resources
CustomInternetGateway :- This resource helps us allocate an internet gateway[IGW] for use with a Virtual private cloud. Post Internet gateway creation, we can attach it to a Virtual private cloud. Tags:- Tags to set naming based on the tagging policy defined.
AttachGateway :- This resource helps us attach an internet gateway, or a virtual private gateway to a our custom VPC, which enables connectivity between the internet and VPC.
As part of VPCGatewayAttachment property we will define.
VpcId :- The one we have selected in parameter "CustomVPC" , we will refer it using "!Ref CustomVPC".
InternetGatewayId:- We will refer it using "!Ref CustomInternetGateway", here we are referring from our previously created resource CustomInternetGateway.
🔳 Outputs: Its always a best practice to print output for your resources.
outputCustomInternetGateway: A reference to the created Internet gateway.
outputAttachGateway: A reference to the created attach gateway resource.

Parameters:
  CustomVPC:
    Description: Select One VPC available in your existing account
    Type: AWS::EC2::VPC::Id
    Default: <Default VPC ID>
Resources:
  CustomInternetGateway:
    Type: AWS::EC2::InternetGateway
    Properties:
      Tags:
        - Key: Name
          Value: VPC_internet_Gateway
  AttachGateway:
    Type: AWS::EC2::VPCGatewayAttachment
    Properties:
      VpcId: !Ref CustomVPC
      InternetGatewayId: !Ref CustomInternetGateway
Outputs:
  outputCustomInternetGateway:
    Description: A reference to the created Internet gateway
    Value: !Ref CustomInternetGateway
  outputAttachGateway:
    Description: A reference to the created attach gateway resource
    Value: !Ref AttachGateway

🔊 To view entire github code click here

1️⃣ Lets validate our template 👨‍💻

aws cloudformation validate-template --template-body file://<file path>

2️⃣ After successfull template verification lets create stack using our template 👨‍💻

aws cloudformation create-stack --stack-name launchandattachinternetgateway --template-body file://<file path>

Note:- If you are not providing default vpc id in parameter then you will have to use below command

aws cloudformation create-stack --stack-name launchandattachinternetgateway --template-body file://<file path> --parameters ParameterKey=CustomVPC,ParameterValue=<VPC ID>

3️⃣ Check if the stack we created via template is completed successfully 👨‍💻

aws cloudformation list-stack-resources --stack-name launchandattachinternetgateway

4️⃣ Describe stack and its resources to view its properties 👨‍💻

aws cloudformation describe-stacks --stack-name launchandattachinternetgateway
aws cloudformation describe-stack-resources --stack-name launchandattachinternetgateway

5️⃣ Check events for stack formation 👨‍💻

aws cloudformation describe-stack-events --stack-name launchandattachinternetgateway

👁‍🗨👁‍🗨 YouTube Tutorial 📽

❗️❗️Important AWS Documentation To Be Viewed❗️❗️

⛔️ AWS::EC2::VPC
⛔️ AWS::EC2::InternetGateway
⛔️ AWS::EC2::VPCGatewayAttachment
⛔️ Condition functions

🥁🥁 Conclusion 🥁🥁

In this blog I have covered 2 scenarios in which we will create with help of VS Code Cloudformation Extension. ✦ Custom VPC.
✦ Internet gateway and associate it to our custom VPC.
I have used AWS CLI command to deploy these template and trust me AWS CLI is the realtime hero and I would suggest you to get acquainted towards it. Stay with me for the next release.

📢 Stay tuned for my next blog.....

🎊So, did you find my content helpful? If you did or like my other content, feel free to buy me a coffee. Thanks. 🎊

💫Cloudformation Series Sequence💫

🔰 Deploy VPC With Internet Gateway & Associate I
🔰 Public, Private Subnet & Route Table Creation & Association II
🔰 Private Subnet,Nat Gateway, Elastic Ip, Private Route Table & Associate III
🔰 NACL, Inbound & Outbound Routes, Security Group & Associate With Subnet IV
🔰 EC2 With Security Group & User Data & Mapping V
🔰 Target Group, Elastic Load Balancer & ELB Listener VI
🔰 Build Web Application Layer With AWS CloudFormation VII

⌛️Realtime Usecases Cloudformation Templates⏳

💨 Schedule Automatic Detection Of Unused AWS EBS Volumes & Notify
💨 Schedule Automatic Detection Of Non Associated AWS Elastic IP's In AWS Account On Weekly Basis And Notify
💨 Schedule Automatic Deregistration Of AWS AMI On Weekly Basis And Notify

👨🏻‍💻Cloudformation Github Repository👨🏻‍💻

Did you find this article valuable?

Support Dheeraj Choudhary by becoming a sponsor. Any amount is appreciated!

See recent sponsors Learn more about Hashnode Sponsors
 
Share this