This project is about getting hands-on experience with deploying a Full Stack web app based on React(w/ TailwindCSS) and deployed using AWS CDK which is a infrastructure-as-a-code solution by AWS.
It was hosted using Amazon Amplify.
Following steps should be run to deploy the web app:
-
aws
,aws-cdk
,npm
andnode
CLI tools should be installed and set in the path. -
Make sure to provide necessary IAM access for
aws
by running:
$ aws configure
- Now we have to set the following enviroment variables:
$ export S3_BUCKET_NAME="[AVAILABLE_BUCKET_NAME]"
$ export INPUT_TABLE_NAME="[INPUT_RECORDS_TABLE]"
$ export OUTPUT_TABLE_NAME="[OUTPUT_RECORDS_TABLE]"
$ export HOST_URL="http://localhost:3000"
- If everything is set, we are ready to deploy our AWS infrastructure. We need the AWS Account Number and Region for running the following:
$ cd infra
$ cdk bootstrap aws://[ACCOUNT-NUMBER]/[REGION]
$ npm run build
$ cdk synth
$ cdk deploy
-
Our S3 bucket, Lambda functions, API gateway, DynamoDb tables and IAM role policy have been created and configured. Now we need the API endpoints of the two lambda functions ("gen-presigned-url-s3" & "save-form-dynamo-db"). These can be found by going to Lambda>Functions>[FUNCTION_NAME]>Configuration>Triggers. Copy the second API endpoint which is for production.
-
Once we have noted the two lambda endpoints, we have to set them as environment variables to build our React app.
$ export REACT_APP_GEN_S3_URL_API_ENDPOINT="[GEN_S3_URL_API_ENDPOINT]"
$ export REACT_APP_SAVE_FORM_DYNAMO_DB_API_ENDPOINT="[SAVE_FORM_DYNAMO_DB_API_ENDPOINT]"
- Finally we can start our web app by running:
$ cd ../app
$ npm install
$ npm start
-
After the submit button is clicked, a PRE-SIGNED URL is generated by our gen-presigned-url-s3 lambda function for uploading the text file to S3. This is required to avoid storing S3 credentials in our React code.
-
Next, the text file is uploaded to S3 bucket and the fields id, input_text and input_file_path are recorded in our DynamoDb INPUT_TABLE by our second lambda function save-form-dynamo-db. This table has been registered with DynamoDB Stream which triggers our third lambda function process-stream-dynamo-db.
-
Now our third lambda function receives the new id and creates a EC2 instance with appropriate configurations and a script with the id to process the output.
-
Once the EC2 instance has been created, it fetches our script process_output.sh from S3 bucket and runs it to:
- query our INPUT_TABLE in Dynamodb by id.
- Fetches the INPUT_FILE from S3 bucket.
- append INPUT_TEXT to the INPUT_FILE and store the output file to the S3 bucket.
- record the resulting id and output_file_path to our DynamoDb OUTPUT_TABLE.
- After finishing the jobs, the VM shuts down resulting in termination of itself.
- https://docs.aws.amazon.com/AWSJavaScriptSDK/v3/latest/introduction/
- https://docs.aws.amazon.com/lambda/latest/dg/configuration-envvars.html#configuration-envvars-runtime
- https://docs.aws.amazon.com/cdk/v2/guide/hello_world.html
- https://medium.com/tomincode/launching-ec2-instances-from-lambda-4a96f1264afb
- https://docs.amplify.aws/react/start/getting-started/introduction/
- https://docs.aws.amazon.com/cdk/api/v2/docs/aws-construct-library.html
- https://github.com/awsdocs/aws-doc-sdk-examples
- https://tailwindcss.com/docs/guides/create-react-app