简体   繁体   English

如何从 AWS CDK 开始在 ASG EC2 实例上运行命令

[英]How do you run commands on ASG EC2 instance start from AWS CDK

How can I add commands to run on instance start when using AWS CDK (Typescript)使用 AWS CDK (Typescript) 时如何添加命令以在实例启动时运行

You can use asg.userData.addCommands(...commands);您可以使用asg.userData.addCommands(...commands);

Example below:下面的例子:

/**
 * Auto-scaling group
 */
const asg = new autoscaling.AutoScalingGroup(this, 'ASG ' + STAGE, {
  instanceType: ec2.InstanceType.of(ec2.InstanceClass.T3, ec2.InstanceSize.MEDIUM),
  keyName: 'my_key',
  machineImage: ec2.MachineImage.genericLinux({ 'eu-west-1': 'your_ami' }),
  updateType: autoscaling.UpdateType.REPLACING_UPDATE,
  minCapacity: 2,
  maxCapacity: 10,
  maxInstanceLifetime: cdk.Duration.days(14),
  vpcSubnets: {
    subnetType: ec2.SubnetType.PUBLIC,
  },
  securityGroup: ec2SecurityGroup,
  vpc,
});

/**
 * Commands to run on instance init
 * Git pull and npm start
 * Needs to be run as ec2-user not root
 */
const commands = [`runuser -l  ec2-user -c 'cd /home/ec2-user/source && git pull && npm start'`];

asg.userData.addCommands(...commands);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM