JINIers
cloudformaiton 이용 네트워크 구성하기(vpc 생성) 본문
cloudformation으로 autoscaling 구축하려다가 리소스 값을 자꾸 변경해줘야하는 귀찮음이 발생해서
스택의 리소스를 연관되게 쓸 수 있는 방법이 없을까 하고 서치하던 중에 베스핀 테크 블로그에서 같은 내용의 게시물을 발견했다.
굉장히 자세히 나와있어서 참고해서 기존 파일을 왕창 수정했다.
네트워크 구성 순서는
- vpc 생성
- lb 생성
- autoscaling 생성
이다.
지짜 베스핀 기술블로그.. 압도적 감사
참고링크 : AWS Cloudformation으로 인프라 구축하기#1 – Network 구성하기(베스핀글로벌테크블로그)
이전버전
https://jiniers.tistory.com/138
추가 및 수정부분 : 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'
'AWS > 이것저것' 카테고리의 다른 글
cloudformaiton 이용 네트워크 구성하기(autoscaling 생성) (0) | 2024.03.29 |
---|---|
cloudformaiton 이용 네트워크 구성하기(LB 생성) (0) | 2024.03.29 |
S3 이용 서버리스 구축 (0) | 2024.03.19 |
create vpc.yaml (0) | 2024.03.14 |
동적 사이트 구축.yaml (0) | 2024.03.14 |
Comments