AWS Serverless With Monorepos

Going serverless with AWS and monorepos

Preparations

$ aws configure

AWS Access Key ID: <Type Your Access Key>
AWS Secret Access Key: <Type Your Secret Key>

Deploying AWS serverless

Deploy the backend

$ cd java-app-backend/
$ sam build
$ sam deploy --guidedSetting default arguments for 'sam deploy'
=========================================
Stack Name [serverless-web-application-java-backend]:
AWS Region [eu-west-1]:
#Shows you resources changes to be deployed and require a 'Y' to initiate deploy
Confirm changes before deploy [y/N]:
#SAM needs permission to be able to create roles to connect to the resources in your template
Allow SAM CLI IAM role creation [Y/n]: y
#Preserves the state of previously provisioned resources when an operation fails
Disable rollback [y/N]:
S3UploaderFunction may not have authorization defined, Is this okay? [y/N]: y
ImageSearch may not have authorization defined, Is this okay? [y/N]: y
Save arguments to configuration file [Y/n]: y
SAM configuration file [samconfig.toml]:
SAM configuration environment [default]:
$ aws s3api create-bucket --bucket BUCKET_NAME --region eu-west-1
$ aws apigateway get-rest-apis{
"items": [
{
"id": "iaadyd31lk", // <-- this is the unique id of the API
"name": "serverless-web-application-java-backend",
...

Deploy the frontend

$ cd frontend/
$ yarn install
$ yarn build
$ aws s3api create-bucket \
--bucket myfrontend-aws-monorepo \
--region eu-west-1 \
--create-bucket-configuration LocationConstraint=eu-west-1
$ aws s3 cp build s3://myfrontend-aws-monorepo --recursive --acl public-read
$ aws s3 website s3://myfrontend-aws-monorepo --index-document index.html
http://BUCKET_NAME.s3-website.eu-west-1.amazonaws.com
$ aws cloudfront create-distribution --origin-domain-name myfrontend-aws-monorepo.s3-website.eu-west-1.amazonaws.com --default-root-object index.html{
"Location": "https://cloudfront.amazonaws.com/2020-05-31/distribution/E26Q8XKJFH6K4G",
"ETag": "E1E0WE2I8CVJPH",
"Distribution": {
"Id": "E26Q8XKJFH6K4G",
"ARN": "arn:aws:cloudfront::890702391356:distribution/E26Q8XKJFH6K4G",
"Status": "InProgress",
"LastModifiedTime": "2021-11-09T20:32:02.716000+00:00",
"InProgressInvalidationBatches": 0,
"DomainName": "dk4ef7pcvfhnv.cloudfront.net", // <--- Cloudfront enabled URL
. . .

Building the backend with CI/CD

sem-version java 17
wget https://github.com/aws/aws-sam-cli/releases/latest/download/aws-sam-cli-linux-x86_64.zip
unzip aws-sam-cli-linux-x86_64.zip -d sam-installation
sudo ./sam-installation/install
checkout
cd java-app-backend
sam build
cache store build-java-app-backend-$SEMAPHORE_GIT_BRANCH .aws-sam
change_in('/java-app-backend')

Continuous deployment for the backend

branch = 'master' AND result = 'passed' AND change_in('/java-app-backend')
sem-version java 17
wget https://github.com/aws/aws-sam-cli/releases/latest/download/aws-sam-cli-linux-x86_64.zip
unzip aws-sam-cli-linux-x86_64.zip -d sam-installation
sudo ./sam-installation/install
checkout
cd java-app-backend
cache restore build-java-app-backend-$SEMAPHORE_GIT_BRANCH
sam deploy --no-confirm-changeset --stack-name serverless-web-application-java-backend --capabilities CAPABILITY_AUTO_EXPAND CAPABILITY_IAM

Continuous deployment for the frontend

sem-version node 16.15.0 
checkout
cd frontend
cache restore
yarn test
change_in('/frontend')
change_in('/frontend') and branch = 'master' AND result = 'passed'
checkout 
cd frontend
cache restore build-frontend-$SEMAPHORE_GIT_BRANCH
aws s3 cp build s3://BUCKET_NAME --recursive --acl public-read

Conclusion

--

--

Supporting developers with insights and tutorials on delivering good software. · https://semaphoreci.com

Love podcasts or audiobooks? Learn on the go with our new app.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store