Let's create a VPC environment with CloudFormation (execution version)
Hello everyone,
this is Okazaki from the SRE team of the System Solutions Department.
last time , I would like to introduce a simple way to use "CloudFormation".
This time, I would like to actually build a VPC from the template file I filled out last time.
Create a stack
First, log in to your development server and register the API key of the user who will execute 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 do not 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
After confirming that the vpc.yml created last time exists, let's immediately build a VPC using the following command.
[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, the creation process will proceed if it is displayed without any errors as shown below.
arn:aws:cloudformation:ap-northeast-1:189461266018:stack/vpc/7b29dce0-4c70-11e9-8b3c-0ee87e6fb924
You can check the status using the command below.
If the display shows "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 a resource
Now, I would like to delete all the resources that I created this time.
If you want to manually delete a resource, you need to go to each resource screen and delete each resource, 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.
Execute the command below, 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.3 1.0.0/16
Once the stack deletion is complete, the following error output will appear.
[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 whether the deletion is actually completed.
I have confirmed that the VPC has disappeared.
Explanation
The following is an explanation of each command.
# aws cloudformation create-stack \ ↑ Declare to use cloudformation with aws cloudformation Command create stack with create-stack, delete-stack to delete > --stack-name vpc \ ↑ stack-name [name] Set the stack name > --region ap-northeast-1 \ ↑ --region [region] Set the region where you want to create the resource > --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 minimal commands.
In addition to this, if you want to change the values of Parameters that were set last time, 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, and specify the parameter ID in ParameterKey and the value in ParameterValue.
summary
This time, we were able to create and delete the stack and each resource from the console.
In this way, you can easily create and delete AWS resources by using CloudFormation.In
the previous and this time, we only talked about creating VPC, but next time we will introduce practical content such as creating EC2, so please try using it as well. .