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:
- CDK CLI Version: 1.1.0 (build 1a11e96)
- Module Version: aws-cdk=1.1.0,@aws-cdk/aws-cloudwatch=1.1.0,@aws-cdk/aws-ec2=1.1.0,@aws-cdk/aws-iam=1.1.0,@aws-cdk/aws-ssm=1.1.0,@aws-cdk/core=1.1.0,@aws-cdk/cx-api=1.1.0,@aws-cdk/region-info=1.1.0,jsii-runtime=Python/3.6.8
- OS: Ubuntu 18.04
- Language: Python
Issue Analytics
- State:
- Created 4 years ago
- Comments:6 (2 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
You should be able to wrap your user data in use
cdk.Fn.base64
. Here’s an example that works for me:Hi, The CDK python example above misses the base64 encoding, the line should be:
user_data= core.Fn.base64(shellCommands.render())
Full code example: