question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Enable adding custom user data to ECS cluster

See original GitHub issue

Currently, adding additional user data to an Autoscaling group and adding it to an ECS cluster is not a smooth experience.

The implementation of autoscalinggroup.addUserData() does not correctly process MIME multitype archives. E.g.:

    const asg = new autoscaling.AutoScalingGroup(this, 'MyFleet', {
      instanceType: new ec2.InstanceTypePair(ec2.InstanceClass.M4, ec2.InstanceSize.Large),
      machineImage: new ec2.AmazonLinuxImage(),
      associatePublicIpAddress: true,
      vpc
    });

    const userData = `
Content-Type: multipart/mixed; boundary="//"
MIME-Version: 1.0

--//
Content-Type: text/cloud-config; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: attachment; filename="cloud-config.txt"

#cloud-config
cloud_final_modules:
- [scripts-user, always]

--//
Content-Type: text/x-shellscript; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: attachment; filename="userdata.txt"

#!/bin/bash
/bin/echo "Hello World" >> /tmp/testfile.txt
--//
`
    asg.addUserData(userData);

    cluster.addAutoScalingGroupCapacity(asg);

Results in the following CFN for LaunchConfiguration:

  MyFleetLaunchConfig5D7F9801:
    Type: AWS::AutoScaling::LaunchConfiguration
    Properties:
      ImageId: ami-01e24be29428c15b2
      InstanceType: m4.large
      AssociatePublicIpAddress: true
      IamInstanceProfile:
        Ref: MyFleetInstanceProfile70A58496
      KeyName: hhh-2
      SecurityGroups:
        - Fn::GetAtt:
            - MyFleetInstanceSecurityGroup774E8234
            - GroupId
      UserData:
        Fn::Base64:
          Fn::Join:
            - ""
            - - |-
                #!/bin/bash

                Content-Type: multipart/mixed; boundary="//"
                MIME-Version: 1.0

                --//
                Content-Type: text/cloud-config; charset="us-ascii"
                MIME-Version: 1.0
                Content-Transfer-Encoding: 7bit
                Content-Disposition: attachment; filename="cloud-config.txt"

                #cloud-config
                cloud_final_modules:
                - [scripts-user, always]

                --//
                Content-Type: text/x-shellscript; charset="us-ascii"
                MIME-Version: 1.0
                Content-Transfer-Encoding: 7bit
                Content-Disposition: attachment; filename="userdata.txt"

                #!/bin/bash
                /bin/echo "Hello World" >> /tmp/testfile.txt
                --//

                echo ECS_CLUSTER=
              - Ref: Ec2ClusterEE43E89D
              - >-2
                 >> /etc/ecs/ecs.config
                sudo iptables --insert FORWARD 1 --in-interface docker+ --destination 169.254.169.254/32 --jump DROP

                sudo service iptables save

                echo ECS_AWSVPC_BLOCK_IMDS=true >> /etc/ecs/ecs.config

When deployed, the User Data on the ec2 instance looks like this:

#!/bin/bash

Content-Type: multipart/mixed; boundary="//"
MIME-Version: 1.0

--//
Content-Type: text/cloud-config; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: attachment; filename="cloud-config.txt"

#cloud-config
cloud_final_modules:
- [scripts-user, always]

--//
Content-Type: text/x-shellscript; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: attachment; filename="userdata.txt"

#!/bin/bash
/bin/echo "Hello World" >> /tmp/testfile.txt
--//

echo ECS_CLUSTER=CustomUserData-Ec2ClusterEE43E89D-1H8KFTDM00P66 >> /etc/ecs/ecs.config
sudo iptables --insert FORWARD 1 --in-interface docker+ --destination 169.254.169.254/32 --jump DROP
sudo service iptables save
echo ECS_AWSVPC_BLOCK_IMDS=true >> /etc/ecs/ecs.config

It should look like this:

Content-Type: multipart/mixed; boundary="36b51cd254c2a10606cc4ea1d7c161a960c25b43fbcf3f2275bfea986b64"
MIME-Version: 1.0

--36b51cd254c2a10606cc4ea1d7c161a960c25b43fbcf3f2275bfea986b64
Content-Disposition: attachment; filename="cloud-config.txt"
Content-Transfer-Encoding: 7bit
Content-Type: text/cloud-config; charset="us-ascii"
Mime-Version: 1.0

#cloud-config
cloud_final_modules:
- [scripts-user, always]

--36b51cd254c2a10606cc4ea1d7c161a960c25b43fbcf3f2275bfea986b64
Content-Disposition: attachment; filename="userdata.txt"
Content-Transfer-Encoding: 7bit
Content-Type: text/x-shellscript; charset="us-ascii"
Mime-Version: 1.0

#!/bin/bash
/bin/echo "Hello World" >> /tmp/testfile.txt
--36b51cd254c2a10606cc4ea1d7c161a960c25b43fbcf3f2275bfea986b64
Content-Type: text/text/x-shellscript; charset="utf-8"
Mime-Version: 1.0


#!/bin/bash
echo ECS_CLUSTER=blargh >> /etc/ecs/ecs.config

--36b51cd254c2a10606cc4ea1d7c161a960c25b43fbcf3f2275bfea986b64--

The above was generated using the ECS-cli with custom user data flag: ecs-cli up --extra-user-data custom-user-data.txt --cluster blargh --capability-iam Though the ECS CLI is able to leverage some go libraries for MIME multipart archive constructing/unpacking.

It would nice to have a smoother API around adding custom user data – not sure if that’s better served through an integration with a higher-level ECS construct or modifying the existing API in the Autoscaling library.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:4
  • Comments:21 (13 by maintainers)

github_iconTop GitHub Comments

1reaction
rsmoguracommented, May 19, 2021

@kristianmandrup, This property is a getter (it’s a function), it can give undefined value if the capacity has not been defined in props, during creation of cluster (that’s one way of defining capacity).

If you use method addCapacity to add a capacity (instead of props), then the result value is an autoscaling group (cluster can be associated with multiple ASG) and you can use it to operate on user data.

As a side note, there’s new set of changes enabling capacity providers, AFIK.

1reaction
rix0rrrcommented, Sep 25, 2019

Custom user data generously supplied by @hoegertn should give you a way to achieve what you need:

https://github.com/aws/aws-cdk/pull/4193

@marcb still interested to hear in what you need to achieve that cfn-init can not cover in the same way that userdata would?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Bootstrapping container instances with Amazon EC2 user data
In the example below, the custom options are added to the Docker daemon configuration file, /etc/docker/daemon.json which is then specified in the user...
Read more >
Create an ECS cluster - Amazon EC2 Spot Workshops
Click Next step · Under Configure cluster for Cluster name, enter EcsSpotWorkshop · Select the checkbox Create an empty cluster · Select the...
Read more >
Amazon ECS container agent configuration - 亚马逊云科技
A list of custom attributes, in JSON format, to apply to your container instances. Using this attribute at instance registration adds the custom...
Read more >
Amazon ECS on RancherOS
RancherOS makes it easy to join your ECS cluster. ... the ECS agent enabled by default, but it can easily be added in...
Read more >
ECS - User Data For EC2 instances - Stack Overflow
It seems that the ECS instance is not registered with the cluster. You need to ensure that the AMIs you use to create...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found