I'm working on deploying a Node.js API using AWS CDK and GitHub Actions, and I want to know if my current process is the best approach. Right now, here's what I do for deployment:
1. **GitHub Actions:**
- Builds the app.
- Creates a Docker image.
- Pushes the Docker image to Amazon ECR with a tag.
- Triggers CDK while passing the image tag as a parameter.
2. **CDK:**
- Sets up IAM roles, networks, and security groups.
- Launches or reboots the instance using a new `ec2.UserData.forLinux()` command that includes the Docker image.
The problem I'm facing is that the user data script only runs when a new instance is created, so when CDK simply reboots the instance, the script doesn't execute. Am I doing this right? Is there a better way to handle this deployment?
3 Answers
Are you considering using ECS Fargate or AWS Lambda? They’re great options for deploying applications without managing your own Docker hosts. I recommend looking into them, especially since you mentioned you’re working on an API.
You’ve got the steps right for deploying through CDK. Just remember that user data runs on the first boot, so reboots won't trigger it. You might want to consider setting up a systemd service to handle it during restarts, or you could use a more container-focused service like Render which simplifies scaling and restarts.
No worries, you're not doing it wrong! Just keep in mind that user data is meant for the initial setup. If you're not familiar with ECS or CDK yet, don't hesitate to ask for help understanding how those services work for your API.

Related Questions
How To: Running Codex CLI on Windows with Azure OpenAI
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