簡體   English   中英

如何將 UserData 添加到 AWS CDK 中的 AWS LaunchTemplate

[英]How to add UserData to an AWS LaunchTemplate in AWS CDK

我一直在將 AWS CDK (Typescript) 用於我的幾個構建,但我發現很難將我的 bash 腳本添加到我的CfnLaunchTemplate的 UserData 。 我的啟動模板的摘錄是這樣的:

    const bootscript = readFileSync('./scripts/test.sh', 'utf8');
    
      // Creation of launch template
      const launchTest = new ec2.CfnLaunchTemplate(this, "launchTest", {
        launchTemplateData: {
          imageId: AMI-test.valueAsString,
          instanceType: "t3a.medium",
          //userData: bootscript,
          //securityGroups: [launchTestSG],
          keyName: props.privateKey,
          iamInstanceProfile: {
            name: props.iamProfile
          }
        },
        launchTemplateName: "LT-Example",
      })

    launchTest.userAddData(bootscript);

我希望這行得通,但行不通。 我已經查看了詳細說明 AWS CDK 在啟動模板方面的問題的文檔,所以我選擇了使用CfnLaunchTemplates的路線,但天哪,它是否會在未來造成問題。 如果有人可以幫助我,我將不勝感激。

PS:我在將我的新安全組也附加到啟動模板時也遇到了問題。 我得到的錯誤是這樣的:

Type 'SecurityGroup' is not assignable to type 'string'

感謝任何人幫助我解決這個問題!

更新:此更新與我為要制作的安全組導入 VPC 的部分有關:

  let vpcDB = ec2.Vpc.fromVpcAttributes(this, "InfraJumpVPC", {
    vpcId: props.vpcId,
    availabilityZones: ["us-east-1a", "us-east-1b", "us-east-1c"],
  });

  // Create a security group for launchTest:
  const launchTestSG = new ec2.SecurityGroup(this, "launchTestSG", {
    vpc: vpcDB,
    allowAllOutbound: true,
    description: "Security group for Testing web server"
  })

抱歉,剛剛更新了錯誤放置的安全組的代碼。

使用 L2 LaunchTemplate構造,它更易於使用。 根據文檔userData接受ec2.UserData一個實例。 userData 的一個簡單示例:

對不起,如果我有任何 TS 語法錯誤,我用 Python 寫:

const my_userdata = UserData.forLinux();
my_userdata.addCommands(["echo hello", "ls ."]);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM