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.

Userdata not populated properly in CfnInstance

See original GitHub issue
  • I’m submitting a …

    • 🪲 bug report
    • 🚀 feature request
    • 📚 construct library gap
    • ☎️ security issue or vulnerability => Please see policy
    • ❓ support request => Please see note at the top of this template.
  • What is the current behavior? I’m attempting to create EC2 instances with userdata to handle post-init configuration using the CfnInstance class. The userdata object is being created via the aws_ec2.UserData constructor.

from aws_cdk import (
    core,
    aws_ec2
)
class ConnectSmtpRouteStack(core.Stack):

    def __init__(self, scope: core.Construct, id: str, **kwargs) -> None:
        super().__init__(scope, id, **kwargs)

        shellCommands = aws_ec2.UserData.for_linux()
        shellCommands.add_commands("command1")
        shellCommands.add_commands("command2")

        ec2Instance1 = CfnInstance(self, 'ec2Instance1',
            image_id='ami-3ecc8f46',
            instance_type='t3.large',
            availability_zone='us-west-2b',
            user_data=shellCommands.render()
        )

The generated CloudFormation code fails with “Invalid BASE64 encoding of user data” error. The relevant YAML output looks like:

  ec2Instance1:
    Type: AWS::EC2::Instance
    Properties:
      AvailabilityZone: us-west-2b
      ImageId: ami-3ecc8f46
      InstanceType: t3.large
      UserData: >-
        #!/bin/bash

        command1

        command2

  • What is the expected behavior (or behavior of feature suggested)? I would expect the generated UserData stanza to include the relevant Base64 encoding blocks. Something like:
      UserData:
        Fn::Base64: !Sub |
           #!/bin/bash
           command1
           command2
  • What is the motivation / use case for changing the behavior or adding this feature? Immediately, I want to create an EC2 instance with additional data volumes which are partitioned, formatted, and mounted when the instance is initialized. There are other post-init workflows that I would like to build later

  • Please tell us about your environment:

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

5reactions
AlexCheemacommented, Jul 26, 2019

I have a related issue, I am trying to embed the IP address of another ec2 instance in the user data of a second ec2 instance with (apologies for the typescript vs python):

    const ec2Instance2 = new ec2.CfnInstance(this, 'ec2Instance2', {
          imageId: 'ami-3ecc8f46',
          instanceType:'t3.large',
          availabilityZone:'us-west-2b',
          userData:`export CDK_IP='${ec2Instance1.attrPrivateIp}'`
    });

This produces

  ec2Instance2:
    Type: AWS::EC2::Instance
    Properties:
      AvailabilityZone: us-west-2b
      ImageId: ami-3ecc8f46
      InstanceType: t3.large
      UserData:
        Fn::Join:
          - ""
          - - export CDK_IP='
            - Fn::GetAtt:
                - ec2Instance1
                - PrivateIp
            - "'"

This fails with a similar error due to lack of "Fn::Base64":

You should be able to wrap your user data in use cdk.Fn.base64. Here’s an example that works for me:

    const dbProxyInstance = new ec2.CfnInstance(this, "DBProxyInstance", {
      instanceType: "t3a.small",
      imageId: "ami-026c8acd92718196b",
      securityGroupIds: [dbProxySecurityGroup.securityGroupId],
      subnetId: vpc.publicSubnets[0].subnetId,
      userData: cdk.Fn.base64(
        `#!/bin/bash -xe 
apt-get -y update
apt-get -y install xorg xfce4 autocutsel chromium-browser
add-apt-repository -y ppa:x2go/stable
apt-get -y install -y x2goserver x2goserver-xsession
apt-get -y install gnome-icon-theme tango-icon-theme`
      )
    });
0reactions
Fzomerdijkcommented, Sep 17, 2019

Hi, The CDK python example above misses the base64 encoding, the line should be:

user_data= core.Fn.base64(shellCommands.render())

Full code example:

from aws_cdk import (
    aws_ec2 as ec2,
    core
)

from .hello_construct import HelloConstruct

class MyStack(core.Stack):

    def __init__(self, scope: core.Construct, id: str, **kwargs) -> None:
        super().__init__(scope, id, **kwargs)

        shellCommands = ec2.UserData.for_linux()
        shellCommands.add_commands(
            "echo \"Install Java 1.8 jdk\"",
            "yum install java-1.8.0-openjdk -y"
        )

        StackStateInstance = ec2.CfnInstance(self, 'StackStateInstance',
            image_id='ami-1234567890',
            instance_type='t3.small',
            availability_zone='eu-central-1a',
            user_data= core.Fn.base64(shellCommands.render()) 
       ) 
Read more comments on GitHub >

github_iconTop Results From Across the Web

class CfnInstance (construct) · AWS CDK
Implements IConstruct , IConstruct , IDependable , IInspectable. A CloudFormation AWS::EC2::Instance . Specifies an EC2 instance.
Read more >
User-data scripts is not running on my custom AMI, but ...
User_data is run only at the first start up. As your image is a custom one, I suppose it have already been started...
Read more >
Deploy bootstrapped EC2 with CDK
The first thing I set in my CDK template are the variables, ... has a reboot in it and the way UserData works...
Read more >
EC2 User Data Example in AWS CDK - Complete Guide
We can see that the user data script we added to our EC2 instance, has installed and booted our apache web server successfully....
Read more >
Amazon EC2 User Data Tutorial - YouTube
Learn about Amazon EC2 User Data, which allows you to bootstrap your instances! If you want to learn more: ...
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