Created
September 19, 2025 03:02
-
-
Save imShakil/b715dd7667e7308ec17c1e2e755dc4c0 to your computer and use it in GitHub Desktop.
a cloudformation template to create vpc and ec2 instance
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| AWSTemplateFormatVersion: '2010-09-09' | |
| Description: 'Create a VPC with public subnet and launch an EC2 instance' | |
| Parameters: | |
| InstanceType: | |
| Description: EC2 instance type | |
| Type: String | |
| Default: t2.micro | |
| AllowedValues: | |
| - t2.micro | |
| - t2.small | |
| - t2.medium | |
| ConstraintDescription: Must be a valid EC2 instance type | |
| KeyName: | |
| Description: Name of an existing EC2 KeyPair to enable SSH access | |
| Type: AWS::EC2::KeyPair::KeyName | |
| ConstraintDescription: Must be the name of an existing EC2 KeyPair | |
| SSHLocation: | |
| Description: IP address range that can SSH to the EC2 instances | |
| Type: String | |
| MinLength: 9 | |
| MaxLength: 18 | |
| Default: 0.0.0.0/0 | |
| AllowedPattern: (\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})/(\d{1,2}) | |
| ConstraintDescription: Must be a valid IP CIDR range of the form x.x.x.x/x | |
| Mappings: | |
| AWSInstanceType2Arch: | |
| t2.micro: | |
| Arch: HVM64 | |
| t2.small: | |
| Arch: HVM64 | |
| t2.medium: | |
| Arch: HVM64 | |
| AWSRegionArch2AMI: | |
| ap-southeast-1: | |
| HVM64: ami-05fd46f12b86c4a6c | |
| Resources: | |
| # VPC | |
| MyVPC: | |
| Type: AWS::EC2::VPC | |
| Properties: | |
| CidrBlock: 10.10.0.0/16 | |
| EnableDnsHostnames: true | |
| EnableDnsSupport: true | |
| Tags: | |
| - Key: Name | |
| Value: MyVPC | |
| # Internet Gateway | |
| MyInternetGateway: | |
| Type: AWS::EC2::InternetGateway | |
| Properties: | |
| Tags: | |
| - Key: Name | |
| Value: MyInternetGateway | |
| # Attach Internet Gateway to VPC | |
| AttachGateway: | |
| Type: AWS::EC2::VPCGatewayAttachment | |
| Properties: | |
| VpcId: !Ref MyVPC | |
| InternetGatewayId: !Ref MyInternetGateway | |
| # Public Subnet | |
| PublicSubnet: | |
| Type: AWS::EC2::Subnet | |
| Properties: | |
| VpcId: !Ref MyVPC | |
| CidrBlock: 10.10.1.0/24 | |
| AvailabilityZone: !Select [0, !GetAZs ''] | |
| MapPublicIpOnLaunch: true | |
| Tags: | |
| - Key: Name | |
| Value: Public Subnet | |
| # Route Table for Public Subnet | |
| PublicRouteTable: | |
| Type: AWS::EC2::RouteTable | |
| Properties: | |
| VpcId: !Ref MyVPC | |
| Tags: | |
| - Key: Name | |
| Value: Public Route Table | |
| # Route to Internet Gateway | |
| PublicRoute: | |
| Type: AWS::EC2::Route | |
| DependsOn: AttachGateway | |
| Properties: | |
| RouteTableId: !Ref PublicRouteTable | |
| DestinationCidrBlock: 0.0.0.0/0 | |
| GatewayId: !Ref MyInternetGateway | |
| # Associate Route Table with Public Subnet | |
| PublicSubnetRouteTableAssociation: | |
| Type: AWS::EC2::SubnetRouteTableAssociation | |
| Properties: | |
| SubnetId: !Ref PublicSubnet | |
| RouteTableId: !Ref PublicRouteTable | |
| # Security Group | |
| InstanceSecurityGroup: | |
| Type: AWS::EC2::SecurityGroup | |
| Properties: | |
| GroupName: MyInstanceSecurityGroup | |
| GroupDescription: Enable SSH access via port 22 | |
| VpcId: !Ref MyVPC | |
| SecurityGroupIngress: | |
| - IpProtocol: tcp | |
| FromPort: 22 | |
| ToPort: 22 | |
| CidrIp: !Ref SSHLocation | |
| - IpProtocol: tcp | |
| FromPort: 80 | |
| ToPort: 80 | |
| CidrIp: 0.0.0.0/0 | |
| Tags: | |
| - Key: Name | |
| Value: MyInstanceSecurityGroup | |
| # EC2 Instance | |
| MyEC2Instance: | |
| Type: AWS::EC2::Instance | |
| Properties: | |
| ImageId: !FindInMap [AWSRegionArch2AMI, !Ref 'AWS::Region', !FindInMap [AWSInstanceType2Arch, !Ref InstanceType, Arch]] | |
| InstanceType: !Ref InstanceType | |
| KeyName: !Ref KeyName | |
| SubnetId: !Ref PublicSubnet | |
| SecurityGroupIds: | |
| - !Ref InstanceSecurityGroup | |
| UserData: | |
| Fn::Base64: !Sub | | |
| #!/bin/bash | |
| yum update -y | |
| yum install -y httpd | |
| systemctl start httpd | |
| systemctl enable httpd | |
| echo "<h1>Hello from CloudFormation!</h1>" > /var/www/html/index.html | |
| Tags: | |
| - Key: Name | |
| Value: MyEC2Instance | |
| Outputs: | |
| VPCId: | |
| Description: VPC ID | |
| Value: !Ref MyVPC | |
| Export: | |
| Name: !Sub ${AWS::StackName}-VPC-ID | |
| PublicSubnetId: | |
| Description: Public Subnet ID | |
| Value: !Ref PublicSubnet | |
| Export: | |
| Name: !Sub ${AWS::StackName}-PublicSubnet-ID | |
| InstanceId: | |
| Description: EC2 Instance ID | |
| Value: !Ref MyEC2Instance | |
| InstancePublicIP: | |
| Description: Public IP address of the EC2 instance | |
| Value: !GetAtt MyEC2Instance.PublicIp | |
| WebsiteURL: | |
| Description: URL for the website | |
| Value: !Sub 'http://${MyEC2Instance.PublicIp}' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment