Deploy a Web Application on Amazon EC2

GETTING STARTED GUIDE

Module 2: Automate Configuration

In this module, you will learn to use  user data to configure an EC2 instance and install software packages on the EC2 instance

Introduction

When you launch an instance in Amazon EC2, you have the option of passing user data to it that can be used to perform common automated configuration tasks and even run scripts after the instance starts. You will use this feature to simplify your application deployment by specifying what software to install and configure on first launch, and copy the sample application to the instance. This module will cover how to create the user data and add it to the EC2 instance created in the previous module.

What You Will Learn

  • How user data is added to the EC2 instance
  • Create the user data to install and configure all required packages on the OS
  • Deploy your web application by copying it to the instance

 Time to Complete

5 minutes

 Module Prereqs

  • AWS Account with administrator-level access**
  • Recommended browser: The latest version of Chrome or Firefox

[**]Accounts created within the past 24 hours might not yet have access to the services required for this tutorial.

Implementation

Adding user data to your EC2 instance

The sample web application hosted in the SampleApp folder, is a Python application that you will be deploying. It requires Nginx and uWSGI to run. To install these components, there are a number of steps to follow. First, you need to install all the OS packages, configure nginx and uwsgi, ensure they are are running, and copy the sample application to the instance. A script file that configures all of these setup steps is provided in SampleApp/configure_amz_linux_sample_app.sh - please have a look at the steps in it if you want to know more about how the instance is configured.

To deploy the web application, you need to add code to CDK that will copy the configuration files and scripts, and the sample app to S3. You will configure the configuration scripts. To do so, add the following code in ec2-cdk-stack.ts below the previous code:

// Use an asset to allow uploading files to S3, and then download it to the EC2 instance as part of the user data

    // --- Sample App ---
    // Upload the sample app  to S3
    const sampleAppAsset = new s3assets.Asset(this, "SampleAppAsset", {
      path: path.join(__dirname, "../../SampleApp"),
    });

    // Allow EC2 instance to read the file
    sampleAppAsset.grantRead(role);

    // Download the file from S3, and store the full location and filename as a variable
    const sampleAppFilePath = ec2Instance.userData.addS3DownloadCommand({
      bucket: sampleAppAsset.bucket,
      bucketKey: sampleAppAsset.s3ObjectKey,
    });

    // --- Sample App ---

    // --- Configuration Script ---
    // Upload the configuration file to S3
    const configScriptAsset = new s3assets.Asset(this, "ConfigScriptAsset", {
      path: path.join(__dirname, "../../SampleApp/configure_amz_linux_sample_app.sh"),
    });

    // Allow EC2 instance to read the file
    configScriptAsset.grantRead(ec2Instance.role);

    // Download the file from S3, and store the full location and filename as a variable
    const configScriptFilePath = ec2Instance.userData.addS3DownloadCommand({
      bucket: configScriptAsset.bucket,
      bucketKey: configScriptAsset.s3ObjectKey,
    });

    // Add a line to the user data to executy the downloaded file
    ec2Instance.userData.addExecuteFileCommand({
      filePath: configScriptFilePath,
      arguments: sampleAppFilePath,
    });

    // --- Configuration Script ---

All the steps will be added to the user data script of your instance, and execute when it first boots up. There is one more step before you can deploy everything: adding output to the CDK stack to make it easier to SSH to the instance. In the infrastructure above, you created an SSH key, which is stored in AWS Secret Manager. To download it to your workstation, you need to retrieve it. You will also need the public IP of the instance, and the SSH command to execute. 

Add the following code to the stack at the bottom:

// Create outputs for connecting

    // Output the public IP address of the EC2 instance
    new cdk.CfnOutput(this, "IP Address", {
      value: ec2Instance.instancePublicIp,
    });

    // Command to download the SSH key
    new cdk.CfnOutput(this, "Download Key Command", {
      value:
        "aws secretsmanager get-secret-value --secret-id ec2-ssh-key/cdk-keypair/private --query SecretString --output text > cdk-key.pem && chmod 400 cdk-key.pem",
    });

    // Command to access the EC2 instance using SSH
    new cdk.CfnOutput(this, "ssh command", {
      value:
        "ssh -i cdk-key.pem -o IdentitiesOnly=yes ec2-user@" +
        ec2Instance.instancePublicIp,
    });

These three outputs will show you the following:

  • How to download the SSH key to access the instance
  • The public IP of the instance
  • An SSH command to access the instance.

You are now ready to deploy the stack.

Conclusion

In this module, you learned how to add user data to an EC2 instance to allow configuring it when it starts up for the first time. You used this to install all the dependencies, configure nginx and uwsgi, and deploy the sample application. In the next module, you will deploy the stack and the sample application.

Up Next: Deploy CDK Stack

Was this page helpful?