Notice
Recent Posts
Recent Comments
Link
«   2024/07   »
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30 31
Tags
more
Archives
Today
Total
관리 메뉴

JINIers

cloudformaiton 이용 네트워크 구성하기(vpc 생성) 본문

AWS/이것저것

cloudformaiton 이용 네트워크 구성하기(vpc 생성)

JINIers 2024. 3. 29. 09:37

cloudformation으로 autoscaling 구축하려다가 리소스 값을 자꾸 변경해줘야하는 귀찮음이 발생해서

스택의 리소스를 연관되게 쓸 수 있는 방법이 없을까 하고 서치하던 중에 베스핀 테크 블로그에서 같은 내용의 게시물을 발견했다.

굉장히 자세히 나와있어서 참고해서 기존 파일을 왕창 수정했다.

네트워크 구성 순서는

이다.

 

지짜 베스핀 기술블로그.. 압도적 감사

 

참고링크 : AWS Cloudformation으로 인프라 구축하기#1 – Network 구성하기(베스핀글로벌테크블로그)

 

AWS Cloudformation으로 인프라 구축하기#1 – Network 구성하기 - BESPIN Tech Blog

이번 글에서는 AWS에서 제공하는 IaC 도구 서비스인 Cloudformation을 이용하여 인프라를 구축하는 내용입니다. What is Cloudformation? AWS 및 서드 파티 리소스를 손쉽게 모델링, 프로비저닝 및 관리할 수

blog.bespinglobal.com

 

이전버전

https://jiniers.tistory.com/138

 

create vpc.yaml

# create vpc vpc, igw, routing table, security group, 1 ec2(install nginx) # create vpc.yaml AWSTemplateFormatVersion: "2010-09-09" Description: "Create 1 VPC, 4 subnet(public, private), 1 igw, 1 routingtable, 1 Security group, 1 ec2 " Parameters: AMI: Typ

jiniers.tistory.com

 

 


추가 및 수정부분 : outputs

 

Outputs :  다른 스택으로 가져오거나(교차 스택 참조를 생성하기 위해), 응답으로 반환하거나(스택 호출을 설명하기 위해), 또는 AWS CloudFormation 콘솔에서 볼 수 있는 출력 값을 선언

# create vpc.yaml

AWSTemplateFormatVersion: "2010-09-09"
Description: "Create 1 VPC, 4 subnet(public, private), 1 igw, 1 routingtable, 1 Security group, 1 ec2 "

Parameters:
  AMI:
    Type: AWS::SSM::Parameter::Value<AWS::EC2::Image::Id>
    Description: 'The ID of the AMI.'
    Default: /aws/service/ami-amazon-linux-latest/al2023-ami-kernel-6.1-x86_64

Resources:
# Create VPC
  vpcA:
    Type: AWS::EC2::VPC
    Properties:
      CidrBlock: 10.1.0.0/16
      EnableDnsSupport: 'true'
      EnableDnsHostnames: 'true'
      InstanceTenancy: default
      Tags:
        - Key: Name
          Value: "vpcA"

# Create Subnet
  PublicSubnetA:
    Type: AWS::EC2::Subnet
    Properties:
      VpcId:
        Ref: vpcA
      CidrBlock: 10.1.10.0/24
      AvailabilityZone: "ap-northeast-2a"
      MapPublicIpOnLaunch: "true"
      Tags:
      - Key: Name
        Value: "Public Subnet AZ1"
  PrivateSubnetA:
    Type: AWS::EC2::Subnet
    Properties:
      VpcId:
        Ref: vpcA
      CidrBlock: 10.1.15.0/24
      AvailabilityZone: "ap-northeast-2a"
      Tags:
      - Key: Name
        Value: "Private Subnet AZ1"
  PublicSubnetB:
    Type: AWS::EC2::Subnet
    Properties:
      VpcId:
        Ref: vpcA
      CidrBlock: 10.1.40.0/24
      AvailabilityZone: "ap-northeast-2b"
      MapPublicIpOnLaunch: "true"
      Tags:
      - Key: Name
        Value: "Public Subnet AZ2"
  PrivateSubnetB:
    Type: AWS::EC2::Subnet
    Properties:
      VpcId:
        Ref: vpcA
      CidrBlock: 10.1.45.0/24
      AvailabilityZone: "ap-northeast-2b"
      Tags:
      - Key: Name
        Value: "Private Subnet AZ2"

# Create Public Route Table
  PublicRouteTable:
    Type: AWS::EC2::RouteTable
    Properties:
      VpcId:
        Ref: vpcA
      Tags:
        - Key: Name
          Value: "PublicRouteTable"
  PublicSubnetARouteTableAssociation:
    Type: AWS::EC2::SubnetRouteTableAssociation
    DependsOn: 
      - "PublicRouteTable"
      - "PublicSubnetA"
    Properties:
      RouteTableId: 
        Ref: PublicRouteTable
      SubnetId:
        Ref: PublicSubnetA
  PublicSubnetCRouteTableAssociation:
    Type: AWS::EC2::SubnetRouteTableAssociation
    DependsOn: 
      - "PublicRouteTable"
      - "PublicSubnetB"
    Properties:
      RouteTableId: 
        Ref: PublicRouteTable
      SubnetId: 
        Ref: PublicSubnetB

# Create Internet GateWay
  igw:
    Type: AWS::EC2::InternetGateway
    Properties:
      Tags:
        - Key: Name
          Value: "igw"
  vpcAInternetGatewayAttachment:
    Type: AWS::EC2::VPCGatewayAttachment
    DependsOn:
      - vpcA
      - igw
    Properties:
      VpcId: !Ref vpcA
      InternetGatewayId: !Ref igw
  vpcAInternetRoute:
    Type: AWS::EC2::Route
    DependsOn: igw
    Properties:
       RouteTableId:
         Ref: PublicRouteTable
       DestinationCidrBlock: 0.0.0.0/0
       GatewayId:
         Ref: igw

# Create Security Group
  vpcASG:
    Type: AWS::EC2::SecurityGroup
    DependsOn: vpcA
    Properties:
      GroupDescription: Web APP Security Group
      GroupName: "Web-SG"
      VpcId: !Ref vpcA
      SecurityGroupIngress:
      - IpProtocol: tcp
        FromPort: 80
        ToPort: 80
        CidrIp: 0.0.0.0/0
      - IpProtocol: icmp
        FromPort: -1
        ToPort: -1
        CidrIp: 0.0.0.0/0
      - IpProtocol: tcp
        FromPort: 22
        ToPort: 22
        CidrIp: 0.0.0.0/0
      - IpProtocol: -1
        CidrIp: 0.0.0.0/0
      Tags:
        - Key: Name
          Value: "web-sg"

# instance
  webec2:
    Type: AWS::EC2::Instance
    DependsOn: 
      - PublicSubnetA
      - vpcASG
    Properties:
      ImageId: !Ref AMI
      KeyName: "test"
      InstanceType: t2.micro
      NetworkInterfaces:
      - AssociatePublicIpAddress: true
        SubnetId: !Ref PublicSubnetA
        DeviceIndex: "0"
        GroupSet:
            - !Ref vpcASG
      Tags:
        - Key: Name
          Value: "web-ec2"
      UserData: 
        Fn::Base64: !Sub |
          #!/bin/bash -xe
          sudo su
          yum install nginx -y
          systemctl enable nginx
          systemctl start nginx

# Outputs :  다른 스택으로 가져오거나(교차 스택 참조를 생성하기 위해), 응답으로 반환하거나(스택 호출을 설명하기 위해), 또는 AWS CloudFormation 콘솔에서 볼 수 있는 출력 값을 선언
Outputs:
  vpcA:
    Description: create vpc
    Value: !Ref vpcA
    Export:
      Name: !Sub '${AWS::StackName}-vpcA'
  PublicSubnetA:
      Description: public subnet AZ1
      Value: !Ref PublicSubnetA
      Export:
        Name: !Sub '${AWS::StackName}-PublicSubnetA'
  PublicSubnetB:
      Description: public subnet AZ2
      Value: !Ref PublicSubnetB
      Export:
        Name: !Sub '${AWS::StackName}-PublicSubnetB'
  PrivateSubnetA:
      Description: private subnet AZ1
      Value: !Ref PrivateSubnetA
      Export:
        Name: !Sub '${AWS::StackName}-PrivateSubnetA'
  PrivateSubnetB:
      Description: private subnet AZ2
      Value: !Ref PrivateSubnetB
      Export:
        Name: !Sub '${AWS::StackName}-PrivateSubnetB'
  vpcASG:
      Description: vpc A Security Group
      Value: !Ref vpcASG
      Export:
        Name: !Sub '${AWS::StackName}-vpcASG'
  webec2:
      Description: vpc A test instance
      Value: !Ref webec2
      Export:
        Name: !Sub '${AWS::StackName}-webec2'

 

 

1. create-vpc.yaml
0.01MB

Comments