AWS EC2 ALB setup with ASG
Published: 2024-03-22 09:53:49

Creating an Amazon Machine Image (AMI) from an EC2 instance and then using it to set up an Application Load Balancer (ALB), a target group, and an Auto Scaling Group (ASG) involves several steps. This guide will walk you through the process, leveraging both the AWS Management Console for a comprehensive understanding.

### Creating an AMI from an EC2 Instance

1. **Using the AWS Management Console:**
   - Navigate to the EC2 Dashboard and select "Instances" from the sidebar.
   - Right-click the instance you wish to create an AMI from and choose "Create Image" from the context menu.
   - In the "Create Image" dialog box, provide a unique name and description for your AMI.
   - By default, AWS will shut down the instance, take snapshots of any attached volumes, create and register the AMI, and then reboot the instance. You can choose "No reboot" if you prefer not to shut down the instance, but be aware that this might affect the file system integrity of the created image.

### Creating an Application Load Balancer (ALB)

1. **Using the AWS Management Console:**
   - Go to the EC2 Dashboard and select "Load Balancers" under the "Load Balancing" section.
   - Click "Create Load Balancer" and choose "Application Load Balancer".
   - Follow the wizard to configure your ALB, including specifying the name, scheme (Internet-facing or Internal), and IP address type (IPv4 or Dualstack). You'll also need to select the VPC, subnets, and security groups for your ALB.

### Creating a Target Group

1. **Using the AWS Management Console:**
   - Navigate to the EC2 Dashboard and select "Target Groups" under the "Load Balancing" section.
   - Click "Create target group" and specify the name, target type (e.g., instance, IP), protocol, and port. You'll also need to select the VPC for your target group.

### Creating an Auto Scaling Group (ASG) with a Launch Template

1. **Using the AWS Management Console:**
   - Go to the EC2 Dashboard and select "Auto Scaling Groups" under the "Auto Scaling" section.
   - Click "Create Auto Scaling group" and follow the wizard to configure your ASG. You'll need to specify the launch template, which includes the AMI ID you created earlier.

### Using AWS Certificate Manager (ACM) to Forward Traffic from HTTP to HTTPS

1. **Request or Import an SSL/TLS Certificate in ACM:**
   - Request a Public Certificate: Navigate to the ACM console and request a public certificate for your domain. You'll need to validate ownership of the domain by adding a CNAME record to your DNS configuration or by using an email validation method.
   - Import a Certificate: If you have an existing certificate from a third party, you can import it into ACM. This includes the certificate body, private key, and certificate chain (if applicable).

2. **Create an HTTPS Listener for Your ALB:**
   - Go to the EC2 Dashboard and select "Load Balancers".
   - Choose your ALB and navigate to the "Listeners" tab.
   - Click "Add listener" and select "HTTPS" as the protocol.
   - For the "Default SSL/TLS certificate", choose "From ACM" and select the certificate you requested or imported.

3. **Configure Redirection from HTTP to HTTPS:**
   - Still in the "Listeners" tab, select the HTTP listener (if it exists) and choose "Edit".
   - Change the "Default action(s)" to "Redirect to" and specify the HTTPS listener you created.
   - You can choose between a temporary (HTTP 302) or permanent (HTTP 301) redirect based on your needs.

4. **Associate the ACM Certificate with Your ALB:**
   - Ensure that the ACM certificate is associated with your ALB as the default SSL/TLS certificate for the HTTPS listener. This step is crucial for the ALB to terminate HTTPS connections using the certificate.

5. **Update DNS Records (Optional):**
   - If you're using a custom domain, update your DNS records to point to your ALB. This step is necessary for clients to resolve your domain name to the ALB's IP address.

### Additional Considerations

- **Security Policy:** When configuring your HTTPS listener, it's recommended to use the latest predefined security policy to ensure the highest level of security.
- **Multiple Certificates:** ALBs support multiple SSL/TLS certificates using Server Name Identification (SNI). This allows you to serve different certificates based on the domain name requested by the client.

By following these steps, you can secure your application by forwarding traffic from HTTP to HTTPS using ACM and configuring your ALB to use HTTPS. This setup not only enhances the security of your application but also simplifies the management of SSL/TLS certificates.