From 1cd8e409a76eb27268a004579780865c6de7d595 Mon Sep 17 00:00:00 2001 From: longbui98 Date: Tue, 24 Oct 2023 21:53:51 +0700 Subject: [PATCH] Create infrastructure --- .circleci/config.yml | 85 ++++++++++++++++++------------------ .circleci/files/backend.yml | 6 +-- .circleci/files/frontend.yml | 3 +- 3 files changed, 46 insertions(+), 48 deletions(-) diff --git a/.circleci/config.yml b/.circleci/config.yml index afcf8b9..8426b3a 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -3,15 +3,12 @@ version: 2.1 commands: destroy-environment: description: Destroy back-end and front-end cloudformation stacks given a workflow ID. - parameters: - # Add parameter here steps: - run: name: Destroy environments when: on_fail command: | - # Your code here - exit 1 + aws cloudformation delete-stack --stack-name udapeople-backend-${CIRCLE_WORKFLOW_ID:0:7} revert-migrations: description: Revert the last migration if successfully run in the current workflow. @@ -129,40 +126,42 @@ jobs: npm audit fix --force npm audit --audit-level=critical -# deploy-infrastructure: -# docker: -# # Docker image here that supports AWS CLI -# steps: -# # Checkout code from git -# - run: -# name: Ensure back-end infrastructure exists -# command: | -# aws cloudformation deploy \ -# --template-file .circleci/files/backend.yml \ -# #--tags project=your-tag \ -# # --stack-name "your back-end stack name with workflow id" \ -# # --parameter-overrides ID="your workflow id" -# exit 1 -# - run: -# name: Ensure front-end infrastructure exist -# command: | -# aws cloudformation deploy \ -# --template-file .circleci/files/frontend.yml \ -# #--tags project=your-tag \ -# # --stack-name "your front-end stack name with workflow id" \ -# # --parameter-overrides ID="your workflow id" -# exit 1 -# - run: -# name: Add back-end ip to ansible inventory -# command: | -# # Your code here -# exit 1 -# - persist_to_workspace: -# root: ~/ -# paths: -# - project/.circleci/ansible/inventory.txt -# # Here's where you will add some code to rollback on failure - + deploy-infrastructure: + docker: + - image: amazon/aws-cli + steps: + - checkout + - run: + name: Install tar and gzip + command: | + yum install -y tar gzip + - run: + name: Ensure back-end infrastructure exists + command: | + aws cloudformation deploy \ + --template-file .circleci/files/backend.yml \ + --tags project=cicd_project \ + --stack-name "udapeople-backend-${CIRCLE_WORKFLOW_ID:0:7}" \ + --parameter-overrides ID="udapeople-backend-${CIRCLE_WORKFLOW_ID:0:7}" + - run: + name: Ensure front-end infrastructure exist + command: | + aws cloudformation deploy \ + --template-file .circleci/files/frontend.yml \ + --tags project=cicd_project \ + --stack-name "udapeople-backend-${CIRCLE_WORKFLOW_ID:0:7}" \ + --parameter-overrides ID="udapeople-backend-${CIRCLE_WORKFLOW_ID:0:7}" + - run: + name: Add back-end ip to ansible inventory + command: | + aws ec2 describe-instances \ + --query 'Reservations[*].Instances[*].PublicIpAddress' \ + --output text >> inventory + - persist_to_workspace: + root: ~/ + paths: + - project/.circleci/ansible/inventory.txt + - destroy-environment # configure-infrastructure: # docker: # # Docker image here that supports Ansible @@ -308,11 +307,11 @@ workflows: requires: [build-backend] - scan-frontend: requires: [build-frontend] - # - deploy-infrastructure: - # requires: [test-frontend, test-backend, scan-frontend, scan-backend] - # filters: - # branches: - # only: [test-feature-branch] + - deploy-infrastructure: + requires: [test-frontend, test-backend, scan-frontend, scan-backend] + filters: + branches: + only: [test-feature-branch] # - configure-infrastructure: # requires: [deploy-infrastructure] # - run-migrations: diff --git a/.circleci/files/backend.yml b/.circleci/files/backend.yml index 2fe943f..74047f6 100644 --- a/.circleci/files/backend.yml +++ b/.circleci/files/backend.yml @@ -28,10 +28,8 @@ Resources: InstanceType: t2.micro SecurityGroups: - Ref: InstanceSecurityGroup - KeyName: udacity # If you use another key pair name, you should change this value to match. - # If this ami id is not available for you, you can find another (https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/finding-an-ami.html) - # Ubuntu 18 or higher works fine - ImageId: ami-068663a3c619dd892 + KeyName: test-key2 + ImageId: ami-0fc5d935ebf8bc3bc Tags: - Key: Name Value: !Sub backend-${ID} diff --git a/.circleci/files/frontend.yml b/.circleci/files/frontend.yml index 2ce0df9..a6227fe 100644 --- a/.circleci/files/frontend.yml +++ b/.circleci/files/frontend.yml @@ -11,7 +11,8 @@ Resources: Type: AWS::S3::Bucket Properties: BucketName: !Sub "udapeople-${ID}" - AccessControl: PublicRead + PublicAccessBlockConfiguration: + BlockPublicPolicy: false WebsiteConfiguration: IndexDocument: index.html ErrorDocument: 404.html