In modern DevOps practices, ensuring that configuration files are updated and consistent across all instances is crucial for maintaining application performance and reliability. AWS CodeDeploy provides a robust solution to automate the deployment of applications, including configuration files, to EC2 instances. This guide will walk you through setting up CodeDeploy jobs to push configuration files to Windows EC2 instances in private subnets.

Prerequisites

Before you begin, ensure you have the following prerequisites:

  1. AWS Account: An active AWS account.
  2. EC2 Instances: Windows EC2 instances running in private subnets.
  3. IAM Roles: Appropriate IAM roles for CodeDeploy and EC2 instances.
  4. AWS CLI: Installed and configured AWS CLI on your local machine.
  5. CodeDeploy Agent: Installed on your Windows EC2 instances.

Step 1: Create an Application in CodeDeploy

  1. Log in to the AWS Management Console.
  2. Navigate to the CodeDeploy service.
  3. Click on Create application.
  4. Provide a name for your application.
  5. Select the Compute platform as EC2/On-Premises.
  6. Click on Create application.

Step 2: Create a Deployment Group

  1. Within your new application, click on Create Deployment Group.
  2. Provide a name for the deployment group.
  3. Select a service role that CodeDeploy will assume during the deployment process. Ensure this role has the necessary permissions.
  4. In the Deployment type, select In-place.
  5. Choose the Amazon EC2 instances by specifying tags, Auto Scaling groups, or manually.
  6. Specify a Deployment configuration (e.g., CodeDeployDefault.OneAtATime).
  7. Configure Load Balancer settings if applicable.
  8. Click on Create Deployment Group.

Step 3: Create an AppSpec File

The AppSpec file defines how CodeDeploy manages each instance during the deployment. Create a file named appspec.yml with the following content:

version: 0.0

os: windows

files:

  – source: /path/to/local/config/files

    destination: C:\path\to\destination\folder

hooks:

  AfterInstall:

    – location: scripts/AfterInstall.ps1

      timeout: 300

      runas: Administrator

  ApplicationStart:

    – location: scripts/ApplicationStart.ps1

      timeout: 300

      runas: Administrator

Step 4: Bundle Your Application and Upload to S3

  1. Zip your configuration files and the AppSpec file.
  2. Upload the zip file to an S3 bucket.

Step 5: Create a Deployment

  1. In the CodeDeploy console, go to your application.
  2. Click on Create Deployment.
  3. Select the deployment group created earlier.
  4. In the Revision type, choose My application is stored in Amazon S3.
  5. Specify the S3 bucket and the zip file containing your application.
  6. Click on Create Deployment.

Step 6: Monitor the Deployment

  1. In the CodeDeploy console, navigate to Deployments.
  2. Select your deployment to view details and monitor the progress.
  3. Verify that the configuration files are pushed to your Windows EC2 instances in the specified directory.

Conclusion

Following these steps, you can easily set up AWS CodeDeploy jobs to push configuration files to Windows EC2 instances in private subnets. This automation ensures that your instances are always up-to-date with the latest configuration, improving the reliability and performance of your applications.