Let's create a VPC environment with CloudFormation (execution version)

table of contents
Hello everyone,
I'm Okazaki, a member of the SRE team in the System Solutions Department.
last time , I would like to introduce a simple way to use "CloudFormation."
This time, I would like to actually create a VPC using the template file I filled out last time.
Create a stack
First, log in to your development server and register the API key for the user who will run CloudFormation as shown below
[root@localhost ~]# aws configure AWS Access Key ID [****************XXXX]: AWS Secret Access Key [****************XXXX]: Default region name [ap-northeast-1]: Default output format [XXXX]:
If you don't have the aws command, install it as follows:
[root@localhost ~]# yum install epel-release [root@localhost ~]# yum install python-pip [root@localhost ~]# pip install awscli
Make sure you have the vpc.yml you created last time, then run the following command to create a VPC
[root@localhost ~]# ls -l total 4 -rw-r--r--. 1 root root 1713 Mar 22 06:20 vpc.yml [root@localhost ~]# aws cloudformation create-stack \ > --stack-name vpc \ > --region ap-northeast-1 \ > --template-body file://./vpc.yml
After execution, if the following message is displayed without any errors, the creation process will proceed
arn:aws:cloudformation:ap-northeast-1:189461266018:stack/vpc/7b29dce0-4c70-11e9-8b3c-0ee87e6fb924
You can check the status with the following command.
When it displays "CREATE_COMPLETE", the resource is complete.
aws cloudformation describe-stacks --stack-name vpc STACKS 2019-03-28T02:12:32.683Z False False arn:aws:cloudformation:ap-northeast-1:189461266018:stack/vpc/f23007a0-50fe-11e9-88b0-0e819627e6da vpc CREATE_COMPLETE DRIFTINFORMATION NOT_CHECKED PARAMETERS PublicSubnetCider 10.31.0.0/24 PARAMETERS ProjectCode test PARAMETERS VPCCider 10.31.0.0/16
Let's check whether the construction of each resource has been completed


The VPC and subnet with the specified identifier were created without any problems
Delete resources
Now, let's delete all the resources we created this time at once.
If you were to delete them manually, you would need to navigate to each resource screen and delete them one by one, but
resources created with CloudFormation can be deleted from a single console.
Return to the development server and issue the following command.
[root@localhost ~]# aws cloudformation delete-stack --stack-name vpc
Now let's check the progress of the deletion.
Run the following command, and if it says "DELETE_IN_PROGRESS", it means that the deletion is in progress.
[root@localhost ~]# aws cloudformation describe-stacks --stack-name vpc STACKS 2019-03-28T02:12:32.683Z 2019-03-28T02:20:53.902Z False False arn:aws:cloudformation:ap-northeast-1:189461266018:stack/vpc/f23007a0-50fe-11e9-88b0-0e819627e6da vpc DELETE_IN_PROGRESS DRIFTINFORMATION NOT_CHECKED PARAMETERS PublicSubnetCider 10.31.0.0/24 PARAMETERS ProjectCode test PARAMETERS VPCCider 10.31.0.0/16
Once the stack deletion is complete, you will see the following error message:
[root@localhost ~]# aws cloudformation describe-stacks --stack-name vpc An error occurred (ValidationError) when calling the DescribeStacks operation: Stack with id vpc does not exist
Now let's check if the deletion was actually completed.
We can confirm that the VPC has disappeared.

Explanation
Here's a step-by-step explanation of each command:
# aws cloudformation create-stack \ ↑ Declare that you will use cloudformation with aws cloudformation Use create-stack to create a stack, or use delete-stack to delete it > --stack-name vpc \ ↑ stack-name [name] Set the name of the stack > --region ap-northeast-1 \ ↑ --region [region] Set the region where you want to create resources > --template-body file://./vpc.yml ↑ --template-body [file URL] Specify the URL of the template file
In this way, you can create a VPC with a minimum number of commands.
If you want to change the values of the parameters you set previously, you can write it as follows.
# aws cloudformation create-stack \ > --stack-name vpc \ > --region ap-northeast-1 \ > --template-body file://./vpc.yml > --parameters \ > ParameterKey=ProjectCode,ParameterValue=test-beyondjapan \ > ParameterKey=VPCCider,ParameterValue="10.23.0.0/16" \ > ParameterKey=PublicSubnetCider,ParameterValue="10.23.0.0/24"
You can declare the use of each parameter in parameters, specify the parameter ID in ParameterKey, and specify the value in ParameterValue
summary
This time, we were able to create and delete stacks and resources from the console.
Using CloudFormation, you can easily create and delete AWS resources.
In this and the previous articles, we only covered creating a VPC, but next time we will introduce practical content such as creating an EC2 instance, so please give it a try.
0