From 8daa229726092b17ad783790f1f38db8465b686f Mon Sep 17 00:00:00 2001 From: Workshop Participant Date: Tue, 3 Dec 2024 20:18:35 +0000 Subject: [PATCH 1/8] add the buildspecs --- buildspec_docker.yml | 36 ++++++++++++++++++++++++++++++++++++ buildspec_unittests.yml | 19 +++++++++++++++++++ 2 files changed, 55 insertions(+) create mode 100644 buildspec_docker.yml create mode 100644 buildspec_unittests.yml diff --git a/buildspec_docker.yml b/buildspec_docker.yml new file mode 100644 index 0000000..9a2f20c --- /dev/null +++ b/buildspec_docker.yml @@ -0,0 +1,36 @@ +version: 0.2 + +env: + variables: + IMAGE_TAG: "latest" # Default value for the tag. Can be overridden. + exported-variables: + - IMAGE_DIGEST + - IMAGE_IDENTIFIER + +phases: + install: + commands: + - npm install + + pre_build: + commands: + - echo "Extracting AWS Account ID from CODEBUILD_BUILD_ARN..." + - export ACCOUNT_ID=$(echo $CODEBUILD_BUILD_ARN | cut -f5 -d ':') + - export repository_name="hotel-app" # Set your repository name here + - echo "Logging into Amazon ECR..." + - aws ecr get-login-password --region $AWS_REGION | docker login --username AWS --password-stdin $ACCOUNT_ID.dkr.ecr.$AWS_REGION.amazonaws.com + + build: + commands: + - echo "Building the Docker image..." + - docker build -t $repository_name:$IMAGE_TAG . + - echo "Tagging the Docker image for ECR..." + - docker tag $repository_name:$IMAGE_TAG $ACCOUNT_ID.dkr.ecr.$AWS_REGION.amazonaws.com/$repository_name:$IMAGE_TAG + - echo "Pushing the Docker image to ECR..." + - docker push $ACCOUNT_ID.dkr.ecr.$AWS_REGION.amazonaws.com/$repository_name:$IMAGE_TAG + + post_build: + commands: + - echo "Retrieving the image digest from ECR..." + - IMAGE_DIGEST=$(aws ecr describe-images --repository-name $repository_name --image-ids imageTag=$IMAGE_TAG --query 'imageDetails[0].imageDigest' --output text) + - IMAGE_IDENTIFIER="${ACCOUNT_ID}.dkr.ecr.${AWS_REGION}.amazonaws.com/${repository_name}@${IMAGE_DIGEST}" \ No newline at end of file diff --git a/buildspec_unittests.yml b/buildspec_unittests.yml new file mode 100644 index 0000000..dbfbde6 --- /dev/null +++ b/buildspec_unittests.yml @@ -0,0 +1,19 @@ +version: 0.2 + +phases: + install: + commands: + - npm install + build: + commands: + - npx jest --ci --collectCoverage --reporters=jest-junit + +reports: + unittests: + files: + - junit.xml + file-format: JUNITXML + codecoverage: + files: + - 'coverage/cobertura-coverage.xml' + file-format: 'COBERTURAXML' \ No newline at end of file From a50c5adc29286caa64a6d5e7271e863c5dfb46a5 Mon Sep 17 00:00:00 2001 From: Workshop Participant Date: Tue, 3 Dec 2024 20:33:49 +0000 Subject: [PATCH 2/8] Added Backend BuildSpec --- buildspec_backend.yml | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 buildspec_backend.yml diff --git a/buildspec_backend.yml b/buildspec_backend.yml new file mode 100644 index 0000000..2f0f25e --- /dev/null +++ b/buildspec_backend.yml @@ -0,0 +1,24 @@ +version: 0.2 + +phases: + pre_build: + commands: + - aws cloudformation validate-template --template-body file://backend.yml + + build: + commands: + - | + if [[ "$BRANCH_NAME" == main* ]]; then + DEPLOY_ENV="stage"; + elif [[ "$BRANCH_NAME" == feature* ]]; then + DEPLOY_ENV="dev"; + else + echo "Error: BRANCH_NAME must start with 'main' or 'feature'." + exit 1; + fi + - aws cloudformation deploy --template-file backend.yml --stack-name BaseInfraStack-$DEPLOY_ENV --capabilities CAPABILITY_IAM CAPABILITY_NAMED_IAM --parameter-overrides Environment=$DEPLOY_ENV --no-fail-on-empty-changeset + - aws cloudformation wait stack-create-complete --stack-name BaseInfraStack-$DEPLOY_ENV + + post_build: + commands: + - aws cloudformation describe-stacks --stack-name BaseInfraStack-$DEPLOY_ENV \ No newline at end of file From 5a5c8e25c841e46678e9efb9527839d4d8a72a33 Mon Sep 17 00:00:00 2001 From: Workshop Participant Date: Tue, 3 Dec 2024 20:46:09 +0000 Subject: [PATCH 3/8] Add App Runner Service --- backend.yml | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/backend.yml b/backend.yml index 2413336..ff03f7c 100644 --- a/backend.yml +++ b/backend.yml @@ -106,7 +106,39 @@ Resources: Threshold: 4 # 80% of 5 ReadCapacityUnits ComparisonOperator: "GreaterThanThreshold" + AppRunnerService: + Type: AWS::AppRunner::Service + Properties: + ServiceName: !Sub "Hotel-${AWS::StackName}-${AWS::Region}" + SourceConfiguration: + AutoDeploymentsEnabled: false + ImageRepository: + ImageIdentifier: !Sub ${AWS::AccountId}.dkr.ecr.${AWS::Region}.amazonaws.com/hotel-app:latest + ImageRepositoryType: ECR + ImageConfiguration: + Port: "8080" + RuntimeEnvironmentVariables: + - Name: DYNAMODB_TABLE_NAME + Value: !Ref RoomsTable + - Name: HOTEL_NAME + Value: !Ref HotelName + AuthenticationConfiguration: + AccessRoleArn: !GetAtt AppRunnerECRAccessRole.Arn + InstanceConfiguration: + InstanceRoleArn: !GetAtt AppRunnerInstanceRole.Arn + + AppRunnerURLSSMParameter: + Type: "AWS::SSM::Parameter" + Properties: + Name: !Sub "/hotelapp/${Environment}/url" + Description: "Hotel app URI" + Type: "String" + Value: !Sub "https://${AppRunnerService.ServiceUrl}" + Outputs: DynamoDBTableName: Description: "Name of the DynamoDB Table" Value: !Ref RoomsTable + AppRunnerServiceUrl: + Description: "URL of the App Runner Service" + Value: !GetAtt AppRunnerService.ServiceUrl From 9b5dc7bd551a509610cdbb82b9ea5cf4f89f951c Mon Sep 17 00:00:00 2001 From: Workshop Participant Date: Tue, 3 Dec 2024 20:55:04 +0000 Subject: [PATCH 4/8] Add test text to index.pug --- views/index.pug | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/views/index.pug b/views/index.pug index 2b7e1b4..f22bc7c 100644 --- a/views/index.pug +++ b/views/index.pug @@ -2,6 +2,6 @@ extends layout block content h1. - Welcome to #{menuTitle} + Welcome to #{menuTitle} test img(src='public/hotel_landing_pic.png' class='img-fluid' style='width:400px; height:400px;') From 9a619e9997dcbedc8b6c98b21de7fe819786f5ca Mon Sep 17 00:00:00 2001 From: Workshop Participant Date: Tue, 3 Dec 2024 20:55:30 +0000 Subject: [PATCH 5/8] Add test text to index.pug --- views/index.pug | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/views/index.pug b/views/index.pug index f22bc7c..2b7e1b4 100644 --- a/views/index.pug +++ b/views/index.pug @@ -2,6 +2,6 @@ extends layout block content h1. - Welcome to #{menuTitle} test + Welcome to #{menuTitle} img(src='public/hotel_landing_pic.png' class='img-fluid' style='width:400px; height:400px;') From fa5a1f0895c6e6a7bcdeea9147221169ff61d629 Mon Sep 17 00:00:00 2001 From: Workshop Participant Date: Tue, 3 Dec 2024 20:57:10 +0000 Subject: [PATCH 6/8] Added Application Tagging --- backend.yml | 3 +++ 1 file changed, 3 insertions(+) diff --git a/backend.yml b/backend.yml index ff03f7c..e8273f9 100644 --- a/backend.yml +++ b/backend.yml @@ -29,6 +29,9 @@ Resources: WriteCapacityUnits: 5 SSESpecification: SSEEnabled: true # Enable server-side encryption for the table + Tags: + - Key: "Project" + Value: !Sub "HotelApp-${Environment}" # IAM Role for App Runner to access DynamoDB AppRunnerInstanceRole: From f6d51be89be16eb68e5c45ecbda3beaa44ba42bc Mon Sep 17 00:00:00 2001 From: Workshop Participant Date: Tue, 3 Dec 2024 21:02:10 +0000 Subject: [PATCH 7/8] Change our Hotel Name --- backend.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/backend.yml b/backend.yml index e8273f9..536a8d3 100644 --- a/backend.yml +++ b/backend.yml @@ -124,7 +124,7 @@ Resources: - Name: DYNAMODB_TABLE_NAME Value: !Ref RoomsTable - Name: HOTEL_NAME - Value: !Ref HotelName + Value: !Sub ${HotelName}-${Environment} AuthenticationConfiguration: AccessRoleArn: !GetAtt AppRunnerECRAccessRole.Arn InstanceConfiguration: From b529dd2228079c904b3c1df7588d4fd6fcf1e12c Mon Sep 17 00:00:00 2001 From: Workshop Participant Date: Tue, 3 Dec 2024 21:05:11 +0000 Subject: [PATCH 8/8] Change our Hotel Name --- backend.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/backend.yml b/backend.yml index 536a8d3..a2e5037 100644 --- a/backend.yml +++ b/backend.yml @@ -124,7 +124,7 @@ Resources: - Name: DYNAMODB_TABLE_NAME Value: !Ref RoomsTable - Name: HOTEL_NAME - Value: !Sub ${HotelName}-${Environment} + Value: !Sub ${HotelName}-${Environment}-2 AuthenticationConfiguration: AccessRoleArn: !GetAtt AppRunnerECRAccessRole.Arn InstanceConfiguration: