This article is a continuation from our last blog post.
Let's create a VPC from the template file we created last time.
Create a stack
First, log in to the development server and register the API key of the user who executes 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 there’s no aws command, install as the following.
[root@localhost ~]# yum install epel-release [root@localhost ~]# yum install python-pip [root@localhost ~]# pip install awscli
Make sure that there is vpc.yml that was created last time and then use the following command to create a VPC.
The status can be checked with the following command.
When “CREATE_COMPLETE” is displayed, the resource is completed.
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
A VPC and a subnet with the specified identifiers must be successfully created.
Delete the resources
Now let’s delete all the resources that were created at once.
When you delete the resources manually, you need to go to each resource’s screen and delete each resource individually, but the resources created with CloudFormation can be deleted from a single console.
Go back to the development server and then run the following command.
[root@localhost ~]# aws cloudformation delete-stack --stack-name vpc
Now, let’s check the deletion progress.
Run the following command and if you see “DELETE_IN_PROGRESS”, it means it’s deleting the resources.
[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 completed, it shows the following error.
[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
The VPC must be deleted at this point.
Let me explain what each command does.
# aws cloudformation create-stack \ ↑ Declare the use of cloudformation by aws cloudformation. create-stack to create a stack, delete-stack to delete a stack. > --stack-name vpc \ ↑ stack-name [name] Set a stack name > --region ap-northeast-1 \ ↑ --region [region] Set a region to create a resource. > --template-body file://./vpc.yml ↑ --template-body [fileURL] Specify the template file URL
You can create a VPC with minimum commands like this.
Also, if you want to change the values of the parameters, you can do the following.
# 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"
Declare each parameter to use with “Parameters”, and set a parameter ID with “ParameterKey”, and set value with “ParameterValue”.
This time, the creation and deletion of a stack and each resource could be performed from the console.
You can easily delete and create AWS resources using CloudFormation like this.