AWS cloud information: how to reuse a bash script placed in a user data parameter when creating EC2?

In Cloudformation, I have two stacks (one nested).

Nested stack "ec2-setup":

{
  "AWSTemplateFormatVersion" : "2010-09-09",

  "Parameters" : {
    // (...) some parameters here

    "userData" : {
      "Description" : "user data to be passed to instance",
      "Type" : "String",
      "Default": ""
    }

  },

  "Resources" : {

    "EC2Instance" : {
      "Type" : "AWS::EC2::Instance",
      "Properties" : {
        "UserData" : { "Ref" : "userData" },
        // (...) some other properties here
       }
    }

  },
  // (...)
}

Now in my main template I want to refer to the nested template presented above and pass the bash script using the parameter userData. Also, I don’t want to embed the contents of the user data in the script because I want to reuse it for multiple ec2 instances (so I don’t want to duplicate the script every time I declare an ec2 instance in my main template).

I tried to achieve this by setting the contents of the script as the default value for the parameter:

{
  "AWSTemplateFormatVersion": "2010-09-09",

  "Parameters" : {
    "myUserData": {
      "Type": "String",
      "Default" : { "Fn::Base64" : { "Fn::Join" : ["", [
        "#!/bin/bash \n",
        "yum update -y \n",

        "# Install the files and packages from the metadata\n",
        "echo 'tralala' > /tmp/hahaha"
      ]]}}
    }
  },
(...)

    "myEc2": {
      "Type": "AWS::CloudFormation::Stack",
      "Properties": {
        "TemplateURL": "s3://path/to/ec2-setup.json",
        "TimeoutInMinutes": "10",
        "Parameters": {
          // (...)
          "userData" : { "Ref" : "myUserData" }
        }

But when I try to start the stack, I get the following error:

" : : ."

, -, , {Fn:: Base64 (...)} , ( base64).

, script ( inline script) ( , ):

"myEc2": {
  "Type": "AWS::CloudFormation::Stack",
  "Properties": {
    "TemplateURL": "s3://path/to/ec2-setup.json",
    "TimeoutInMinutes": "10",
    "Parameters": {
      // (...)
      "userData" : { "Fn::Base64" : { "Fn::Join" : ["", [
        "#!/bin/bash \n",
        "yum update -y \n",

        "# Install the files and packages from the metadata\n",
        "echo 'tralala' > /tmp/hahaha"
        ]]}}
    }

userData script /, .

bash script / ?

+4
3

bash script EC2, CloudFormation:

1.

: , ( YAML JSON / inline):

  AWSTemplateFormatVersion: "2010-09-09"
  Parameters:
    myUserData:
      Type: String
      Default: |
        #!/bin/bash
        yum update -y
        # Install the files and packages from the metadata
        echo 'tralala' > /tmp/hahaha
(...)
  Resources:
    myEc2:
      Type: AWS::CloudFormation::Stack
      Properties
        TemplateURL: "s3://path/to/ec2-setup.yml"
        TimeoutInMinutes: 10
        Parameters:
          # (...)
          userData: !Ref myUserData

(Fn::Base64, Fn::Sub, , Ref Fn::GetAtt script) EC2:

  AWSTemplateFormatVersion: "2010-09-09"
  Parameters:
    # (...) some parameters here
    userData:
      Description: user data to be passed to instance
      Type: String
      Default: ""    
  Resources:
    EC2Instance:
      Type: AWS::EC2::Instance
      Properties:
        UserData:
          "Fn::Base64":
            "Fn::Sub": !Ref userData
        # (...) some other properties here
  # (...)

2. script S3

bash script S3, script, script EC2 :

  AWSTemplateFormatVersion: "2010-09-09"
  Parameters:
    # (...) some parameters here
    ScriptBucket:
      Description: S3 bucket containing user-data script
      Type: String
    ScriptKey:
      Description: S3 object key containing user-data script
      Type: String
  Resources:
    EC2Instance:
      Type: AWS::EC2::Instance
      Properties:
        UserData:
          "Fn::Base64":
            "Fn::Sub": |
              #!/bin/bash
              aws s3 cp s3://${ScriptBucket}/${ScriptKey} - | bash -s
        # (...) some other properties here
  # (...)

3. script

, -, troposphere , , CloudFormation, / . , "" , , .

+6

, . " ". , .

, . , , . API- coudformation . , . (, bash script) . "" , Jinja ; . - , , . , , .

, , .

, . , , .

+1

, . , , "", , , "bash script" ( ).

, - " " - . ( ), base64 script .

, , . , , , " ", - , .

. paramaters-stack.json :

{
  "AWSTemplateFormatVersion": "2010-09-09",
  "Description": "Outputs/returns parameter values",


  "Conditions" : {
    "alwaysFalseCondition" : {"Fn::Equals" : ["aaaaaaaaaa", "bbbbbbbbbb"]}
  },

  "Resources": {
    "FakeResource" : {
      "Type" : "AWS::EC2::EIPAssociation",
      "Condition" : "alwaysFalseCondition",
      "Properties" : {
        "AllocationId" :  { "Ref": "AWS::NoValue" },
        "NetworkInterfaceId" : { "Ref": "AWS::NoValue" }
      }
    }
  },

  "Outputs": {
    "ec2InitScript": {
      "Value":
      { "Fn::Base64" : { "Fn::Join" : ["", [
        "#!/bin/bash \n",
        "yum update -y \n",

        "# Install the files and packages from the metadata\n",
        "echo 'tralala' > /tmp/hahaha"
      ]]}}

    }
  }
}

, :

{
  "AWSTemplateFormatVersion": "2010-09-09",

   "Resources": { 

    "myParameters": {
      "Type": "AWS::CloudFormation::Stack",
      "Properties": {
        "TemplateURL": "s3://path/to/paramaters-stack.json",
        "TimeoutInMinutes": "10"
      }
    },

    "myEc2": {
      "Type": "AWS::CloudFormation::Stack",
      "Properties": {
        "TemplateURL": "s3://path/to/ec2-setup.json",
        "TimeoutInMinutes": "10",
        "Parameters": {
          // (...)
          "userData" : {"Fn::GetAtt": [ "myParameters", "Outputs.ec2InitScript" ]}
        }
     }
  }
}

, 60 , 60 / .

0
source

Source: https://habr.com/ru/post/1665554/


All Articles