gpt4 book ai didi

amazon-web-services - 编写脚本并通过aws cloudformation在EC2中运行

转载 作者:行者123 更新时间:2023-12-03 07:31:57 25 4
gpt4 key购买 nike

我可以在cloudformation中创建EC2实例。但我想在 EC2 内编写一个 .sh 脚本,并在实例启动时运行一次。谁能指导我实现它?

最佳答案

您可以使用 AWS::CloudFormation::Init 并直接传递 shell 命令(请参阅第一个示例),或者例如写入文件(第二个示例)

{
"Resources": {
"MyEc2Instance": {
"Type": "AWS::EC2::Instance",
"Metadata": {
"AWS::CloudFormation::Init": {
"do_something": {
"commands": {
"foobar": {
"command": "echo FOO > /etc/motd"
}
}
}
}
},
"Properties": {
...
}
}
}
}

{
"Resources": {
"MyEc2Instance": {
"Type": "AWS::EC2::Instance",
"Metadata" : {
"AWS::CloudFormation::Init" : {
"do_something" : {
"files" : {
"/usr/local/bin/myscript.sh" : {
"content" : { "Fn::Join" : ["", [
"#!/bin/sh\n",
"ls -al\n"
]]},
"mode" : "000755",
"owner" : "root",
"group" : "root"
}
}
}
}
},
"Properties": {
}
}
}
}

关于amazon-web-services - 编写脚本并通过aws cloudformation在EC2中运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58540047/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com