简体   繁体   English

Typescript CDK,如何将文件添加到 ec2 实例?

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

I know how to use Here docs to run scripts on a newly launched instance such as我知道如何使用 Here docs 在新启动的实例上运行脚本,例如

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

but I do not not want to do this for configuration files since they are git managed.但我不想对配置文件执行此操作,因为它们是 git 管理的。

So how do I include text files in a newly launched instance?那么如何在新启动的实例中包含文本文件呢? Amazon seems to want you to use S3 according to the Assets documentation here https://docs.aws.amazon.com/cdk/latest/guide/assets.html but I think there should be a way in native CDK to do this I just can't find it in the API docs.亚马逊似乎希望您根据此处的资产文档使用 S3 https://docs.aws.amazon.com/cdk/latest/guide/assets.html但我认为本机 CDK 中应该有一种方法可以做到这一点我只是在 API 文档中找不到它。

Ec2 construct has an init parameter that you can exploit as described here Ec2 构造有一个 init 参数,您可以按照此处所述进行利用

In order to do that you have to leverage both the CloudFormationInit class and the InitFile class .为了做到这一点,你必须同时利用了CloudFormationInit类InitFile类

This will allow you to provide files while your instance is being started from a number of sources.这将允许您在从多个源启动实例时提供文件。 In order to to contain the size of your template is recommended to store assets in an S3 bucket if the file is too big.如果文件太大,为了包含模板的大小,建议将资产存储在 S3 存储桶中。

Putting it all together you should write down something like this:把它们放在一起,你应该写下这样的东西:

    // 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