AWS CloudFormation 创建堆栈与部署
AWS CloudFormation create-stack vs deploy
有人可以清楚地向我解释 AWS CLI Cloudformation create-stack
和 deploy
命令之间的区别和优先级吗?对我来说,他们似乎做同样的事情并部署资源。
为什么当您 运行 来自 cli 的部署命令时,创建堆栈没有可执行更改集,而文档说:
Deploys the specified AWS CloudFormation template by creating and then executing a change set. The command terminates after AWS CloudFormation executes the change set. If you want to view the change set before AWS CloudFormation executes it, use the --no-execute-changeset flag.
create-stack
只能在您知道要创建新堆栈时使用。如果你想更新堆栈,你必须使用不同的命令等。如果你正在编写 (ug) 批处理文件来帮助 运行 你的 cloudformation,这可能是一个真正的痛苦。
deploy
是一种可以更好地利用变更集的功能 - 您不必知道堆栈是否存在,只需 运行 部署,该工具就会找出它需要什么做。使用 --no-execute-changeset
,如果您决定要在应用更改之前查看更改,它实际上会为您提供所需的命令。
这似乎是在 2016 年 11 月引入的,可能是在发布更改集的时间左右。
我假设 deploy
只是 'syntactic sugar' CreateChangeSet
、CreateStack
和 UpdateStack
api 方法。
注意虽然deploy
is in the CLI, it is not in the API reference.
我假设 deploy
是首选,除非需要明确审查变更集。如果不使用 deploy
,您可能需要 create-change-set
然后决定是创建还是更新堆栈。在这种情况下,部署就像一个堆栈 "upsert".
我不再偷懒并检查了代码,是的 - deploy
最终是从 CLI 使用 cloudformation 的更好方式。实现是 here and here. Note that as of today the ability to control rollback behaviour doesn't existing for deploy
per this issue.
当您更改参数默认值(在我的例子中是 LatestAmi
)时,请注意 deploy
命令的奇怪行为。
$ cat ec2.yaml
AWSTemplateFormatVersion: "2010-09-09"
Parameters:
LatestAmi:
Type: AWS::SSM::Parameter::Value<AWS::EC2::Image::Id>
Default: /aws/service/canonical/ubuntu/server/20.04/stable/current/amd64/hvm/ebs-gp2/ami-id
Resources:
MyInstance:
Type: AWS::EC2::Instance
Properties:
ImageId: !Ref LatestAmi
InstanceType: t2.micro
Tags:
- Key: Name
Value: cfn-deploy
$ aws cloudformation deploy --template-file ec2.yaml --stack-name cfn-deploy
Waiting for changeset to be created..
Waiting for stack create/update to complete
Successfully created/updated stack - cfn-deploy
$ cat ec2.yaml
AWSTemplateFormatVersion: "2010-09-09"
Parameters:
LatestAmi:
Type: AWS::SSM::Parameter::Value<AWS::EC2::Image::Id>
Default: /aws/service/ami-amazon-linux-latest/amzn2-ami-hvm-x86_64-gp2
Resources:
MyInstance:
Type: AWS::EC2::Instance
Properties:
ImageId: !Ref LatestAmi
InstanceType: t2.micro
Tags:
- Key: Name
Value: cfn-deploy
$ aws cloudformation deploy --template-file ec2.yaml --stack-name cfn-deploy
Waiting for changeset to be created..
No changes to deploy. Stack cfn-deploy is up to date
$ aws --version
aws-cli/2.5.2 Python/3.9.11 Linux/5.15.0-23-generic exe/x86_64.ubuntu.22 prompt/off
如果您使用 update-stack
命令,实例将替换为请求的 AMI。
有人可以清楚地向我解释 AWS CLI Cloudformation create-stack
和 deploy
命令之间的区别和优先级吗?对我来说,他们似乎做同样的事情并部署资源。
为什么当您 运行 来自 cli 的部署命令时,创建堆栈没有可执行更改集,而文档说:
Deploys the specified AWS CloudFormation template by creating and then executing a change set. The command terminates after AWS CloudFormation executes the change set. If you want to view the change set before AWS CloudFormation executes it, use the --no-execute-changeset flag.
create-stack
只能在您知道要创建新堆栈时使用。如果你想更新堆栈,你必须使用不同的命令等。如果你正在编写 (ug) 批处理文件来帮助 运行 你的 cloudformation,这可能是一个真正的痛苦。
deploy
是一种可以更好地利用变更集的功能 - 您不必知道堆栈是否存在,只需 运行 部署,该工具就会找出它需要什么做。使用 --no-execute-changeset
,如果您决定要在应用更改之前查看更改,它实际上会为您提供所需的命令。
这似乎是在 2016 年 11 月引入的,可能是在发布更改集的时间左右。
我假设 deploy
只是 'syntactic sugar' CreateChangeSet
、CreateStack
和 UpdateStack
api 方法。
注意虽然deploy
is in the CLI, it is not in the API reference.
我假设 deploy
是首选,除非需要明确审查变更集。如果不使用 deploy
,您可能需要 create-change-set
然后决定是创建还是更新堆栈。在这种情况下,部署就像一个堆栈 "upsert".
我不再偷懒并检查了代码,是的 - deploy
最终是从 CLI 使用 cloudformation 的更好方式。实现是 here and here. Note that as of today the ability to control rollback behaviour doesn't existing for deploy
per this issue.
当您更改参数默认值(在我的例子中是 LatestAmi
)时,请注意 deploy
命令的奇怪行为。
$ cat ec2.yaml
AWSTemplateFormatVersion: "2010-09-09"
Parameters:
LatestAmi:
Type: AWS::SSM::Parameter::Value<AWS::EC2::Image::Id>
Default: /aws/service/canonical/ubuntu/server/20.04/stable/current/amd64/hvm/ebs-gp2/ami-id
Resources:
MyInstance:
Type: AWS::EC2::Instance
Properties:
ImageId: !Ref LatestAmi
InstanceType: t2.micro
Tags:
- Key: Name
Value: cfn-deploy
$ aws cloudformation deploy --template-file ec2.yaml --stack-name cfn-deploy
Waiting for changeset to be created..
Waiting for stack create/update to complete
Successfully created/updated stack - cfn-deploy
$ cat ec2.yaml
AWSTemplateFormatVersion: "2010-09-09"
Parameters:
LatestAmi:
Type: AWS::SSM::Parameter::Value<AWS::EC2::Image::Id>
Default: /aws/service/ami-amazon-linux-latest/amzn2-ami-hvm-x86_64-gp2
Resources:
MyInstance:
Type: AWS::EC2::Instance
Properties:
ImageId: !Ref LatestAmi
InstanceType: t2.micro
Tags:
- Key: Name
Value: cfn-deploy
$ aws cloudformation deploy --template-file ec2.yaml --stack-name cfn-deploy
Waiting for changeset to be created..
No changes to deploy. Stack cfn-deploy is up to date
$ aws --version
aws-cli/2.5.2 Python/3.9.11 Linux/5.15.0-23-generic exe/x86_64.ubuntu.22 prompt/off
如果您使用 update-stack
命令,实例将替换为请求的 AMI。