Configure AMI and Launch Template

Create AMI (Amazon Machine Image)

1. Verify EC2 Instance

# Check running application
pm2 status
pm2 list

# Verify configuration files
ls -la /home/ubuntu/nodejs-app/<YOUR_PROJECT_FOLDER>
cat /etc/systemd/system/nodejs-app.service

ami

2. Create AMI from EC2 Console

  1. Access EC2 Console

    • Select configured instance
    • Actions → Image and templates → Create image
  2. Fill AMI Information

# Basic settings
Image name: nodejs-app-ami-v1
Description: AMI for Node.js application with PM2 and configured environment

# Advanced settings
No reboot: ✓ (Don't restart instance when creating AMI)

create-ami

3. Verify AMI Status

  1. Check AMI status
    • EC2 Console → Images → AMIs
    • Wait for status to change from “pending” to “available”
    • Then name the AMI nodejs-app-ami-v1 verify-ami

Important Notes:

  1. Ensure application is running stably before creating AMI

Create Launch Template

1. Configure Launch Template

  1. EC2 Console > Launch Templates > Create launch template launch-template
  2. Basic information:
    • Name: nodejs-app-template-v1
    • Description: Launch template for Node.js application
    • Auto Scaling guidance: ✓ Check launch-template

2. Detailed Configuration

  1. Application and OS Images:

    • My AMIs: Select created AMI (nodejs-app-ami-v1)
  2. Instance type:

    • t2.medium launch-template
  3. Key pair:

    • Select created key pair
  4. Network settings:

    • Subnet: Select Don’t include in launch template
    • FireWall: Select existing security group
    • Common security groups: Select public-ec2-sg created in Security Group

launch-template

  1. Advanced details:
    • User data:
#!/bin/bash
# Reload systemd to receive new config
sudo systemctl daemon-reload

# Enable service to auto-start on boot
sudo systemctl enable nodejs-app

# Start service
sudo systemctl start nodejs-app

# Check status
sudo systemctl status nodejs-app

launch-template

Carefully check these points:

  1. AMI must be completed before creating template
  2. Security group must have correct inbound rules (ports 9000, 22)
  3. IAM role must have necessary permissions
  4. User data script must be accurate

3. Test Launch Template

  1. Select created template
  2. Actions > Launch instance from template
  3. Verify:
    • Instance launches successfully
    • Node.js application runs correctly
    • Can access port 9000
  4. Terminate test instance after verification

References

  1. Creating an AMI
  2. Launch Templates
  3. User Data Scripts

Complete!