I am new to Pulumi and Typescript. I am trying to ...
# getting-started
h
I am new to Pulumi and Typescript. I am trying to deploy ecs on EC2. I need to install some rexray plugins on the EC2 instances and therefore need to use userData to get that completed. I have been able to get a simple ecs on ec2 up and running without userData, but when I add userData in the stack fails. Following is a example of what I have so far.
Copy code
import * as pulumi from "@pulumi/pulumi";
import * as aws from "@pulumi/aws";
import * as awsx from "@pulumi/awsx";


// Step 1: Define the Networking for our service.  Will join the appropriate POD network.  Pod02 or Pod03.
// Use an existing VPC, subnets, and gateways.
const existingVpc = awsx.ec2.Vpc.fromExistingIds("existingVpc", {
    vpcId: "vpc-xxxxxxxxxxxxxx",
    publicSubnetIds: ["subnet-1111111111111111", "subnet-22222222222222222", "subnet-3333333333333333"],
    //privateSubnetIds: ["subnet-22222222222222222", "subnet-33333333333333333"],
    internetGatewayId: "igw-xxxxxxxxxxxxxxxx",
    //natGatewayIds: ["nat-00000000000000000", "nat-11111111111111111"],
})

// Step 2: Create an ECS EC2 cluster.
const cluster = new awsx.ecs.Cluster("EBS-to-EFS", {
    vpc: existingVpc,
    tags: {
        "Name": "EBS-to-EFS-cluster",
    },
});

export const userData = 
 `#!/bin/bash`

// Comment out the following lines of the userData script till I get userData to not crash the stack.

// yum install -y amazon-efs-utils
// yum install -y nfs-utils
// yum install -y aws-cfn-bootstrap
// /opt/aws/bin/cfn-init -v --region \${AWS::Region} --stack \${AWS::StackName} --resource ContainerInstances
// /opt/aws/bin/cfn-signal -e \$? --region \${AWS::Region} --stack \${AWS::StackName} --resource ECSAutoScalingGroup
// #open file descriptor for stderr
// exec 2>>/var/log/ecs/ecs-agent-install.log
// set -x
// #verify that the agent is running
// until curl -s <http://localhost:51678/v1/metadata>
// do
// sleep 1
// done
// #install the Docker volume plugin
// docker plugin install rexray/efs REXRAY_PREEMPT=true EFS_REGION=\${AWS::Region} EFS_SECURITYGROUPS=\${EFSSecurityGroup} --grant-all-permissions
// docker plugin install rexray/ebs REXRAY_PREEMPT=true EBS_REGION=\${AWS::Region} --grant-all-permissions
// #restart the ECS agent
// stop ecs 
// start ecs`


const asg = cluster.createAutoScalingGroup("asg", {
    templateParameters: { minSize: 1 },
    subnetIds: existingVpc.publicSubnetIds,
    launchConfigurationArgs: { 
        instanceType: "t2.medium", 
        associatePublicIpAddress: true, 
        userData:  userData,
        }, 
    });

const nginx = new awsx.ecs.EC2Service("nginx", {
    cluster,
    taskDefinitionArgs: {
        containers: {
            nginx: {
                image: "nginx",
                memory: 128,
                networkListener: { port: 80},
            },
        },
    },
    desiredCount: 2,
});


// Export interesting fields to make them easy to use:
export const vpcId = existingVpc.id;
//export const vpcPrivateSubnetIds = existingVpc.privateSubnetIds;
export const vpcPublicSubnetIds = existingVpc.publicSubnetIds;
If I comment out userData: userData in the Autoscaling Group Definition I have a successful build. When I uncomment the line a include userData in the Autoscaling group the stack errors out with the following.
Copy code
Diagnostics:
  aws:cloudformation:Stack (asg):
    error: 1 error occurred:
        * creating urn:pulumi:dev::ecs-cpass::awsx:x:ecs:Cluster$awsx:x:autoscaling:AutoScalingGroup$aws:cloudformation/stack:Stack::asg: 1 error occurred:
        * error waiting for CloudFormation Stack creation: failed to create CloudFormation stack, rollback requested (ROLLBACK_COMPLETE): ["The following resource(s) failed to create: [Instances]. Rollback requested by user." "Received 0 SUCCESS signal(s) out of 1.  Unable to satisfy 100% MinSuccessfulInstancesPercent requirement"]
 
  pulumi:pulumi:Stack (ecs-cpass-dev):
    error: update failed
Any hints or code examples that I could follow to get this functioning properly? I have spent way to many hours trying to diagnose how to fix this.
w
I think it’s because
//
is not a comment in bash. So the bash script being deployed via user-data is failing and causing the instance to essentially fail. Try changing the userdata script to use
#
for the commented out line.
h
Yeah. I am starting with one line in the userData variable. To keep it simple. The // lines are comments in typeScript. The userData has one line in it…
#!/bin/bash
. When and If I get that working I will add the following lines into userData.
b
@handsome-napkin-75099 does the AWS console give you more info about why the stack isn't coming up heathily? I think you might need to install the ecs agent in your AMI somehow
h
What I see is the 0 success in the console. Maybe I need to try to see more what is happening with the instance, but I am not sure how to see that. All I am seeing is the AutoScalingGroup stack definition and the events within it.
@billowy-army-68599 My understanding is that if I do not specify an AMI image that Pulumi chooses a ECS Optimized AMI which includes the ecs agent.
b
you should be able to go into the ECS console and get more info
I'm free 1-2 PST if you wanna jump on a call together?
h
That would be fantastic.
I will initiate a stack again and see what I can see in the ECS Console.
b
@handsome-napkin-75099 grab some time from here: https://calendly.com/lbriggs58/30min
h
Lee I was not able to schedule time for today. I set it up for tomorrow at 3:00 PM Central Time.
b
yeah sorry, my calendar just filled up, is tomorrow any good?
h
I have it on my calendar. I am looking to TF as a work around.
b
😞 we can get you over the line I'm sure!
h
Yeah, I am sure we can. Just I have spent to much time on this and I have not found any examples for ECS on EC2. Looking at https://github.com/anrim/terraform-aws-ecs. Which looks like exactly what I need.
It looks like the instance never gets added to the Cluster. I see the EC2 instance up and running, but nothing in the cluster. I suspect that my userData is clobbering the initialization of the instance to join the cluster.
b
I would recommend base64 encoding your userdata before passing it to your instance - give that a try and see how it goes
👍 1