AWS Cloudformation

2.2k views Asked by At

Well, I am trying to find a cloudformation template in AWS.

Where would I need to create three VPC's with single subnet and instance in it. Where you have internetgateway in it with 2 one-way from vpc to gateway and one two-way connection like this:

enter image description here

3

There are 3 answers

0
wjordan On

You can leverage the AWS Quick Start's Amazon VPC Architecture template to get started quickly with a boilerplate VPC architecture. This AWS-supported template creates a single VPC containing both a public (2-way) and private (1-way, outbound Internet only) subnet within each specified Availability Zone (you provide 2-4 Availability Zones as Parameters). I would recommend starting with the Quick Start, then later customizing to better fit your specific needs if necessary.

For your use case, you could specify 2 Availability Zones, then use the Private Subnets in each AZ for SubnetA and SubnetB, and the Public Subnet in one of the AZs for SubnetC.

(Note: I recommend against creating 3 separate VPCs for a single application. Distinct Subnets provide adequate network isolation, creating 3 separate VPCs duplicates many unnecessary additional resources such as Internet Getways, and there is a limit of 5 VPCs per region per AWS account.)

Here's a full working example that uses the Quick Start template directly as a nested stack:

Launch Stack

Description: Create a VPC with 2 private and 1 public subnets, with an EC2 instance in each.
Mappings:
  RegionMap:
    us-east-1:
      # amzn-ami-hvm-2016.09.1.20161221-x86_64-gp2
      "opal": "ami-9be6f38c"
      "rstudio": "ami-9be6f38c"
Parameters:
  InstanceType:
    Description: EC2 instance type
    Type: String
    Default: t2.medium
    AllowedValues: [t2.nano, t2.micro, t2.small, t2.medium, t2.large, t2.xlarge, t2.2xlarge,
      m4.large, m4.xlarge, m4.2xlarge, m4.4xlarge, m4.10xlarge, m4.16xlarge,
      c4.large, c4.xlarge, c4.2xlarge, c4.4xlarge, c4.8xlarge,
      r4.large, r4.xlarge, r4.2xlarge, r4.4xlarge, r4.8xlarge, r4.16xlarge]
    ConstraintDescription: Please choose a valid instance type.
  AvailabilityZones:
    Description: List of 2 Availability Zones to use for the subnets in the VPC.
    Type: "List<AWS::EC2::AvailabilityZone::Name>"
  KeyPairName:
    Description: Public/private key pair to provide SSH access to the EC2 instances.
    Type: "AWS::EC2::KeyPair::KeyName"
Resources:
  VPCStack:
    Type: AWS::CloudFormation::Stack
    Properties:
      TemplateURL: 'https://s3.amazonaws.com/quickstart-reference/aws/vpc/latest/templates/aws-vpc.template'
      Parameters:
        AvailabilityZones: !Join [',', !Ref AvailabilityZones]
        KeyPairName: !Ref KeyPairName
        NumberOfAZs: 2
  SecurityGroup:
    Type: AWS::EC2::SecurityGroup
    Properties:
      GroupDescription: VPC Security Group
      VpcId: !GetAtt VPCStack.Outputs.VPCID
  OpalServer1:
    Type: AWS::EC2::Instance
    Properties:
      ImageId: !FindInMap [ RegionMap, !Ref "AWS::Region", opal]
      InstanceType: !Ref InstanceType
      SecurityGroupIds: [!Ref SecurityGroup]
      SubnetId: !GetAtt VPCStack.Outputs.PrivateSubnet1AID
      KeyName: !Ref KeyPairName
  OpalServer2:
    Type: AWS::EC2::Instance
    Properties:
      ImageId: !FindInMap [ RegionMap, !Ref "AWS::Region", opal]
      InstanceType: !Ref InstanceType
      SecurityGroupIds: [!Ref SecurityGroup]
      SubnetId: !GetAtt VPCStack.Outputs.PrivateSubnet2AID
      KeyName: !Ref KeyPairName
  RStudioClient:
    Type: AWS::EC2::Instance
    Properties:
      ImageId: !FindInMap [ RegionMap, !Ref "AWS::Region", rstudio]
      InstanceType: !Ref InstanceType
      SecurityGroupIds: [!Ref SecurityGroup]
      SubnetId: !GetAtt VPCStack.Outputs.PublicSubnet1ID
      KeyName: !Ref KeyPairName
0
Utkarsh Sharma On

You can use readymade templates provided by AWS and modify them as per requirement I am sharing link for your reference.

Note : Cloudformation is Json based take care of syntax

Link :- https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/sample-templates-services-us-west-2.html#d0e207425

0
OmegaOdie On

There is an awesome tool called Console Recorder for AWS, it's a browser plugin for Chrome or Firefox. It copies the actions you perform on the AWS console and converts them into CF, Terraform, Js calls ( because everything in aws is an API ). I'd advise building small chunks as it's very beta. It can't do all the heavy lifting, but it can take the pain out of turning a Network diagram into a set of ordered managable IaC scripts. They have a Git page.