Hey everyone! I'm working on a project that consists of two AWS Lambda functions (built with Java) and a simple frontend application using vanilla JavaScript. I'm currently using Infrastructure as Code (IaC), specifically AWS SAM, but I'm open to other options. My main goals are to provision the infrastructure (including Lambda and API Gateway), deploy the Lambda functions, retrieve the public API Gateway URLs for each function, inject those URLs into the frontend app (as environment variables or in a configuration file), and finally build and publish the frontend, possibly to S3 or CloudFront. I want this workflow to function both on my local machine and in a CI/CD pipeline. Any advice on automating this process? Are there any best practices or preferred patterns within the AWS ecosystem for dynamically integrating deployed API URLs into a frontend application? I'd really appreciate any tips or examples you can share!
5 Answers
Given that you're using AWS heavily, it might be worth considering a CI/CD solution that operates outside of AWS. Things like GitHub Actions or GitLab CI can help you accomplish all your goals while also allowing for integration with other services as your project scales. Plus, they can run locally too.
You can output the necessary information in your CloudFormation stacks and create a script to read those outputs. This script can save them in a place your frontend can access, like a config.json file in S3. Just be aware that this method is not secure for sensitive information, though.
A simple approach is to output the API Gateway URLs after deployment and inject them into your frontend configuration as part of the build process. In your CI/CD pipeline, capture those outputs, update a config.js or .env.js file, and then build and push to S3 or CloudFront. It keeps everything clean and repeatable!
It's definitely doable! Personally, I'd recommend using AWS CDK instead of SAM. With CDK, you can use S3BucketDeployment to include a JSON file that has the API URLs, which your frontend can read. Just remember to think about how you'll secure those APIs, as they'll be publicly accessible.
If I were in your shoes, I'd go with GitHub Actions for CI/CD and maybe use cdktf or just stick with CDK for your IaC.
Related Questions
Set Wordpress Featured Image Using Javascript
How To Fix PHP Random Being The Same
Why no WebP Support with Wordpress
Replace Wordpress Cron With Linux Cron
Customize Yoast Canonical URL Programmatically
[Centos] Delete All Files And Folders That Contain a String