Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/hotelchange #16

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 35 additions & 19 deletions backend.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
AWSTemplateFormatVersion: "2010-09-09"
Description: CloudFormation template for Hotel App with DynamoDB and App Runner, without networking.
Description: CloudFormation template for Hotel app with DynamoDB and App Runner, without networking.

Parameters:
HotelName:
@@ -11,7 +11,6 @@ Parameters:
Default: dev
Description: Enter a name for the Environment. Default is dev, this is used later in the workshop to tag the environments


Resources:
# DynamoDB Table for Rooms
RoomsTable:
@@ -29,8 +28,11 @@ 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
# IAM Role for AppRunner to access DynamoDB
AppRunnerInstanceRole:
Type: AWS::IAM::Role
Properties:
@@ -88,25 +90,39 @@ Resources:
- "sts:AssumeRole"
ManagedPolicyArns:
- "arn:aws:iam::aws:policy/service-role/AWSAppRunnerServicePolicyForECRAccess"

#Sample Alarm - we will use this later to prove AWS CodePipeline Condition Gates
DynamoDBReadCapacityAlarm:
Type: "AWS::CloudWatch::Alarm"

AppRunnerService:
Type: AWS::AppRunner::Service
Properties:
AlarmName: !Sub "HighReadCapacityAlarm-${AWS::StackName}-${AWS::Region}"
AlarmDescription: "Alarm if DynamoDB read capacity exceeds 80% of provisioned units"
Namespace: "AWS/DynamoDB"
MetricName: "ConsumedReadCapacityUnits"
Dimensions:
- Name: "TableName"
Value: !Ref RoomsTable
Statistic: "Average"
Period: 60
EvaluationPeriods: 1
Threshold: 4 # 80% of 5 ReadCapacityUnits
ComparisonOperator: "GreaterThanThreshold"
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: !Sub ${HotelName}-${Environment}-2
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
24 changes: 24 additions & 0 deletions buildspec_backend.yml
Original file line number Diff line number Diff line change
@@ -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
36 changes: 36 additions & 0 deletions buildspec_docker.yml
Original file line number Diff line number Diff line change
@@ -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}"
19 changes: 19 additions & 0 deletions buildspec_unittests.yml
Original file line number Diff line number Diff line change
@@ -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'