简体   繁体   English

在 EC2 上将 shell 脚本添加到 lambda function

[英]add shell script to lambda function on EC2

I have a lambda function that boots a machine on EC2 triggered by a file uploaded on S3 bucket.我有一个 lambda function,它通过上传到 S3 存储桶的文件触发在 EC2 上启动一台机器。 I would like to run a shell command that is in that machine after the boot, but I failed to do so.我想在启动后运行该机器中的 shell 命令,但我没有这样做。 Any thoughts of what I can do?想知道我能做什么吗?

import boto3

region = 'us-east-1'
instances = ['i-079e6065f959e151a']

def lambda_handler(event, context):
    ec2 = boto3.client('ec2', region_name=region)
    ec2.start_instances(InstanceIds=instances)

On the Amazon EC2 instance, store your script in:在 Amazon EC2 实例上,将您的脚本存储在:

/var/lib/cloud/scripts/per-boot/

Any scripts in that directory will be automatically run each time that the instance boots (or 'Starts').每次实例启动(或“启动”)时,该目录中的任何脚本都会自动运行。

When the instance has finished its work, it should perform a shutdown with:当实例完成其工作时,它应该执行关闭:

sudo shutdown now -h

This will return the instance to a Stopped state.这会将实例返回到 Stopped state。

See: Auto-Stop EC2 instances when they finish a task - DEV Community请参阅: 完成任务时自动停止 EC2 实例 - DEV Community

暂无
暂无

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

相关问题 aws lambda 在不同的 ec2 机器上创建不同的初始化脚本 - aws lambda to create different init script on different ec2 machine Lambda function 搜索所有正在运行的 EC2 实例,如果不存在则添加标签“名称”,然后将标签添加到它的关联卷? - Lambda function to search for all running EC2 instances, add tag 'Name' if not present, and then add tags to it's associated volumes? 从 EC2 实例调用 AWS Lambda function - Invoke a AWS Lambda function from EC2 instance 如何将 EC2 事件配置为 java-lambda function - How to configure to EC2 event to a java-lambda function 如何将 AWS lambda 环境变量传递给用户数据脚本(脚本将在 ec2 启动期间触发) - How can I pass AWS lambda environement variable into userdata script ( script will fire during ec2 spinup) ansible 脚本“SHELL”与在 ubuntu 机器 20.04 EC2 上直接运行 Shell 命令的细微差别? - The subtle difference in an ansible script "SHELL" and the directly running Shell commands on an ubuntu machine 20.04 EC2? 为什么我的 lambda function 不能访问 S3 和 SQS 而同一 VPC 中的 EC2 实例可以访问? - Why can my lambda function not access S3 and SQS when an EC2 instance in the same VPC can? 在 EC2 上使用 SSM 运行命令执行 Lambda Function 时访问被拒绝 - Access Denied when executing Lambda Function with SSM Run Command on EC2 让 AWS lambda 访问 EC2 上的 PostgreSQL - Give AWS lambda access to PostgreSQL on EC2 EC2 恢复快照 BOTO3 lambda - EC2 Restore Snapshot BOTO3 lambda
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM