1. Deploying to EC2 instances with AWS CodeDeploy DeploymentGroup

    TypeScript

    Deploying an application to EC2 instances using AWS CodeDeploy involves a few steps in Pulumi. Firstly, you'll create an EC2 instance where your application will reside. Then, you'll configure AWS CodeDeploy to manage deployments to this instance.

    Below is the Pulumi program written in TypeScript. This program sets up an EC2 instance, creates a CodeDeploy application, and configures a deployment group. Go through the comments in the code for details on each step. Before running this program, ensure you have AWS credentials configured for Pulumi.

    import * as pulumi from '@pulumi/pulumi'; import * as aws from '@pulumi/aws'; // Create an EC2 instance that we want to deploy to. const ec2Instance = new aws.ec2.Instance('web-server', { // Specify the AMI ID. Use the latest Amazon Linux 2 AMI for your region. // Replace 'ami-xxxxxx' with an actual AMI ID. ami: 'ami-xxxxxx', // Choose an appropriate instance type. instanceType: 't2.micro', // Associate a key pair for SSH access (create or import one into AWS first). keyName: 'my-key-pair', // Use an existing security group or create a new one that allows inbound traffic on the ports you require. // `vpcSecurityGroupIds` replaces `securityGroups` which is deprecated. vpcSecurityGroupIds: ['sg-xxxxxxxx'], // Replace with your actual security group ID. // Optionally, add any tags to help you identify this resource. tags: { Name: 'web-server-instance', }, }); // Create a new CodeDeploy application. const app = new aws.codedeploy.Application('app', { name: 'MyApp', // Specify the compute platform, change if you're deploying to ECS or Lambda. computePlatform: 'Server', // Valid values are `Server`, `Lambda`, or `ECS`. }); // Create a new IAM role for the CodeDeploy instance that allows it to be managed by CodeDeploy. const instanceRole = new aws.iam.Role('instanceRole', { assumeRolePolicy: aws.iam.assumeRolePolicyForPrincipal({ Service: 'ec2.amazonaws.com', }), }); // Attach the AWS managed CodeDeploy policy to the role. const rolePolicyAttachment = new aws.iam.RolePolicyAttachment('rolePolicyAttachment', { role: instanceRole.name, policyArn: 'arn:aws:iam::aws:policy/service-role/AWSCodeDeployRole', // This is the AWS managed policy ARN. }); // Create an instance profile to associate the role with the EC2 instance. const instanceProfile = new aws.iam.InstanceProfile('instanceProfile', { role: instanceRole.name, }); // Update the EC2 instance to use the newly created instance profile. const ec2InstanceWithRole = new aws.ec2.Instance('web-server-with-role', { ...ec2Instance, iamInstanceProfile: instanceProfile.name, }, { dependsOn: [instanceProfile] }); // Create a CodeDeploy deployment group to manage deployments. const depGroup = new aws.codedeploy.DeploymentGroup('depGroup', { appName: app.name, serviceRoleArn: instanceRole.arn, // Set up the deployment configuration (e.g., CodeDeployDefault.OneAtATime). deploymentConfigName: 'CodeDeployDefault.OneAtATime', deploymentGroupName: 'MyDeploymentGroup', ec2TagFilters: [{ key: 'Name', value: 'web-server-instance', type: 'KEY_AND_VALUE', // Possible values are `KEY_ONLY`, `VALUE_ONLY`, `KEY_AND_VALUE`. }], }); // Export the EC2 instance's public DNS and the CodeDeploy application and deployment group names. export const instancePublicDns = ec2InstanceWithRole.publicDns; export const codeDeployAppName = app.name; export const codeDeployDeploymentGroupName = depGroup.deploymentGroupName;

    Before running this Pulumi program, make sure to replace placeholders such as 'ami-xxxxxx' and 'sg-xxxxxxxx' with actual values for your environment, including the AMI ID and security group ID. Also, ensure that the SSH key pair 'my-key-pair' exists in your AWS account or replace it with your own.

    The above program starts by creating an EC2 instance. It's configured to use a specified AMI and instance type appropriate for our deployment. We then create a CodeDeploy Application which represents our application and a DeploymentGroup to manage the deployment configurations and deployment processes.

    When you run this program with Pulumi, it will provision all the necessary AWS resources configured as per the code and allow you to deploy your application to the EC2 instance using AWS CodeDeploy.

    After successful execution, you can see the exported values, which include the public DNS of the EC2 instance (to allow access, if needed) and names of the CodeDeploy application and deployment group, which you can use to manage further deployments.