簡體   English   中英

Typescript CDK,如何將文件添加到 ec2 實例?

[英]Typescript CDK, how to add files to ec2 instance?

我知道如何使用 Here docs 在新啟動的實例上運行腳本,例如

instance.addUserData(
      bash << EOF
      #!/usr/bin/bash 
      ...
      EOF,
);

但我不想對配置文件執行此操作,因為它們是 git 管理的。

那么如何在新啟動的實例中包含文本文件呢? 亞馬遜似乎希望您根據此處的資產文檔使用 S3 https://docs.aws.amazon.com/cdk/latest/guide/assets.html但我認為本機 CDK 中應該有一種方法可以做到這一點我只是在 API 文檔中找不到它。

Ec2 構造有一個 init 參數,您可以按照此處所述進行利用

為了做到這一點,你必須同時利用了CloudFormationInit類InitFile類

這將允許您在從多個源啟動實例時提供文件。 如果文件太大,為了包含模板的大小,建議將資產存儲在 S3 存儲桶中。

把它們放在一起,你應該寫下這樣的東西:

    // prepare the file as an s3 asset
     const s3Asset = new Asset(this, 'SampleSingleFileAsset', {
        path: './local/path/tofile/file.extension',
    });

    // prepare the file as an asset and put it in the cfinit
    const initData = CloudFormationInit.fromElements(
InitFile.fromExistingAsset('/destination/path/filename.extension', s3Asset, {})
);

    // create the EC2 Instance with the initData parameter
    const ec2Instance = new Instance(this, 'ec2-instance', {
                   
        //other parameters...

        init: initData
    });

暫無
暫無

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

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