Cloudformation (7) (1)

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

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 📽

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

🥁🥁 Conclusion 🥁🥁

We have seen how to stream CloudWatch logs from EC2 to cloudwatch by using cloudwatch agent configuration wizard . This wizard automates our task to create json file based on our requirement with user friendly option.

📢 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

Dheeraj_Pic1 (2)

Author - Dheeraj Choudhary

I am an IT Professional with 11+ years of experience specializing in DevOps & Build and Release Engineering, Software configuration management in automating, build, deploy and release. I blog about AWS and DevOps on my YouTube channel, which focuses on content such as, AWS, DevOps, open source, AI-ML and AWS community activities.

RELATED ARTICLES

Comments are closed.