AWS cli 在没有 appspec 文件的情况下创建部署

Mos*_* GZ 3 amazon-web-services aws-cli aws-code-deploy

有没有一种方法可以在不使用文件的情况下运行AWS Codedeployappspec.yml

我正在寻找一种方法来创建 100% 纯命令行方式来运行 create-deployment,而不使用 S3 存储桶中的任何 yml 文件

Mat*_*ijs 7

我在网上找到了使用 YAML 输入但没有使用 JSON 输入的示例。虽然 YAML 有其优点,但有时 JSON 在我看来更容易使用(例如在 bash/gitlab CI 脚本中)。

aws deploy使用 JSON 调用而不使用 S3 并在变量中构造 Appspec 内容的方法:

APPSPEC=$(echo '{"version":1,"Resources":[{"TargetService":{"Type":"AWS::ECS::Service","Properties":{"TaskDefinition":"'${AWS_TASK_DEFINITION_ARN}'","LoadBalancerInfo":{"ContainerName":"react-web","ContainerPort":3000}}}}]}' | jq -Rs .)
Run Code Online (Sandbox Code Playgroud)

请注意jq -Rs .最后的内容:内容应该是 JSON-as-String,而不是实际 JSON 的一部分。使用jq我们转义 JSON。根据需要替换变量(AWS_TASK_DEFINITION_ARN、ContainerName 和 ContainerPort 等)

REVISION='{"revisionType":"AppSpecContent","appSpecContent":{"content":'${APPSPEC}'}}'
Run Code Online (Sandbox Code Playgroud)

最后我们可以使用新版本创建部署:

aws deploy create-deployment --application-name "${AWS_APPLICATION_NAME}" --deployment-group-name "${AWS_DEPLOYMENT_GROUP_NAME}" --revision "$REVISION"
Run Code Online (Sandbox Code Playgroud)

在 aws-cli/2.4.15 上测试