Home > Net >  AWS CDK Pass raw text to UserData
AWS CDK Pass raw text to UserData

Time:05-08

I am trying to pass data to a 3rd party appliance/ami using AWS CDK V2 for Python

The appliance expects a piece of configuration data to be passed in via user data.

In the AWS Console, you are able to input userdata as text.

If using raw CloudFormation directly, I could do the following

UserData:
    Fn::Base64: !Ref 'APIKEY'

I am unsure of how to do this with the CDK. When I do the following with the CDK

  instance.add_user_data(api_token)

I get

UserData:
    Fn::Base64: |-
      #!/bin/bash
      api-token-material

Is there anyway to replicate passing the material as text?

CodePudding user response:

So this isn't well-documented, in my opinion, but the answer is fairly trivial. Use the custom UserData static method.https://docs.aws.amazon.com/cdk/api/v1/docs/@aws-cdk_aws-ec2.UserData.html#static-customcontent

 per_instance_user_data = ec2.UserData.custom(api_token)
 ec2.Instance(...,
           
            user_data=per_instance_user_data,
      
        )

The CF Template generated looks like

      UserData:
       Fn::Base64: api-token-material
  • Related