Some quick blogs on using CloudFormation to provision AWS infrastructure using code. I have attempted to put descriptive comments in line to explain what each sub-unit of code is doing.
This YAML code creates a VPC, an internet gateway, and attaches the gateway to the VPC.
Some definitions:
VPC
Amazon Virtual Private Cloud (Amazon VPC) enables you to launch AWS resources into a virtual network that you’ve defined. This virtual network closely resembles a traditional network that you’d operate in your own data center, with the benefits of using the scalable infrastructure of AWS.
Internet Gateway
An internet gateway is a horizontally scaled, redundant, and highly available VPC component that allows communication between your VPC and the internet.
VPC Gateway Attachment
Attaches an internet gateway, or a virtual private gateway to a VPC, enabling connectivity between the internet and the VPC.
The code is below:
AWSTemplateFormatVersion: '2010-09-09' # ## The Description section (optional) enables you to include comments about your template. # Description: Create VPC, an internet gateway, and attach the internet gateway to the VPC # ## Parameters section to customize your templates # Parameters: VPCName: Description: Name of the VPC Type: String Default: "MyVPC" MinLength: '1' MaxLength: '30' AllowedPattern: '^[a-zA-Z]+[0-9a-zA-Z\-]*$' ConstraintDescription: Must contain alphabets and/or numbers. VpcCIDR: Description: Please enter the IP range (CIDR notation) for this VPC Type: String Default: 10.0.0.0/16 MinLength: '10' MaxLength: '18' AllowedPattern: "(\\d{1,3})\\.(\\d{1,3})\\.(\\d{1,3})\\.(\\d{1,3})/(\\d{1,2})" ConstraintDescription: Must be a valid CIDR range of the form x.x.x.x/x. # ## Resources created by the stack # Resources: # ## Create the VPC ## ## Uses the intrinsic function Ref to get the value of the VPC Name ## from parameters above # VPC: Type: AWS::EC2::VPC Properties: CidrBlock: !Ref VpcCIDR EnableDnsSupport: true EnableDnsHostnames: true Tags: - Key: Name Value: !Ref VPCName # ## Create the IGW # InternetGateway: Type: AWS::EC2::InternetGateway Properties: Tags: - Key: Name Value: !Ref VPCName # ## Connect the IGW to the VPC # InternetGatewayAttachment: Type: AWS::EC2::VPCGatewayAttachment Properties: InternetGatewayId: !Ref InternetGateway VpcId: !Ref VPC # ## Resources created by the stack ## ## Uses the intrinsic function Sub to get the stack name ## from parameters above and substitute it into the name of ## the internet gateway # Outputs: VPC: Description: Name of the VPC Value: !Ref VPC Export: Name: !Sub '${AWS::StackName}' InternetGateway: Description: Internet Gateway Value: !Ref InternetGateway Export: Name: !Sub '${AWS::StackName}-InternetGateway'