gpt4 book ai didi

amazon-web-services - AWS Systems Manager 如何在自动化步骤之间添加延迟

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

我有一个 AMI 自动化模板,用于创建自定义 AMI。

在 AMI 创建期间,我想在关闭之前添加一个小的延迟,以允许 EC2 实例完成一些首次启动优化。

下面是我的模板,我需要在步骤 DoStep1stopInstance 之间有一个延迟:

AWSTemplateFormatVersion: "2010-09-09"
Description: "SSM Automation Document for creating a new AMI"
Parameters:
SubnetId:
Description: "ID of subnet to use for launching EC2 instance"
Type: "AWS::EC2::Subnet::Id"
SecurityGroupIds:
Description: "The IDs of security groups that are permitted access to EC2 instance"
Type: "List<AWS::EC2::SecurityGroup::Id>"
Outputs:
AmiAutomationDocumentName:
Value: !Ref "AmiAutomationDoc"
Resources:
AutomationRole:
Type: "AWS::IAM::Role"
Properties:
Path: "/"
AssumeRolePolicyDocument:
Statement:
- Action:
- "sts:AssumeRole"
Effect: "Allow"
Principal:
Service:
- "ec2.amazonaws.com"
- "ssm.amazonaws.com"
Version: "2012-10-17"
Policies:
- PolicyName: "PassRole"
PolicyDocument:
Version: "2012-10-17"
Statement:
- Action:
- "iam:PassRole"
Effect: "Allow"
Resource: "*"
ManagedPolicyArns:
- "arn:aws:iam::aws:policy/service-role/AmazonSSMAutomationRole"
InstanceProfileRole:
Type: "AWS::IAM::Role"
Properties:
Path: "/"
AssumeRolePolicyDocument:
Statement:
- Action:
- "sts:AssumeRole"
Effect: "Allow"
Principal:
Service:
- "ec2.amazonaws.com"
- "ssm.amazonaws.com"
Version: "2012-10-17"
Policies:
- PolicyName: "PassRole"
PolicyDocument:
Version: "2012-10-17"
Statement:
- Action:
- "iam:PassRole"
Effect: "Allow"
Resource: "*"
ManagedPolicyArns:
- "arn:aws:iam::aws:policy/service-role/AmazonEC2RoleforSSM"
InstanceProfile:
Type: "AWS::IAM::InstanceProfile"
Properties:
Path: "/"
Roles:
- !Ref "InstanceProfileRole"
AmiAutomationDoc:
Type: "AWS::SSM::Document"
Properties:
DocumentType: "Automation"
Content:
schemaVersion: "0.3"
description: "Create a new AMI"
parameters:
SourceAmiId:
type: "String"
description: "AMI to patch"
TargetAmiName:
type: "String"
description: "Name of new AMI"
assumeRole: !GetAtt "AutomationRole.Arn"
mainSteps:
- name: "startInstance"
action: "aws:runInstances"
timeoutSeconds: 360
maxAttempts: 1
onFailure: "Abort"
inputs:
ImageId: "{{ SourceAmiId }}"
InstanceType: "m4.large"
- name: "DoStep1"
action: "aws:runCommand"
timeoutSeconds: 360
maxAttempts: 1
onFailure: "Abort"
inputs:
ImageId: "{{ SourceAmiId }}"
InstanceType: "m4.large"
- name: "stopInstance"
action: "aws:changeInstanceState"
maxAttempts: 1
onFailure: "Continue"
inputs:
InstanceIds:
- "{{ startInstance.InstanceIds }}"
DesiredState: "stopped"
- name: "createImage"
action: "aws:createImage"
maxAttempts: 1
onFailure: "Continue"
inputs:
InstanceId: "{{ startInstance.InstanceIds }}"
ImageName: "{{ TargetAmiName }}"
ImageDescription: "AMI based on base image {{ SourceAmiId }}"
- name: "terminateInstance"
action: "aws:changeInstanceState"
maxAttempts: 1
onFailure: "Continue"
inputs:
InstanceIds:
- "{{ startInstance.InstanceIds }}"
DesiredState: "terminated"
outputs:
- createImage.ImageId
- startInstance.InstanceIds
DoStep1:
Type: "AWS::SSM::Document"
Properties:
DocumentType: "Command"
Content:
schemaVersion: "1.2"
description: "Schedule scripts"
runtimeConfig:
aws:runPowerShellScript:
properties:
- runCommand:
- myScript.ps1

最佳答案

aws: sleep

最近,AWS 发布了新的 Amazon EC2 Systems Manager 自动化操作。其中之一是 aws:sleep

aws:sleep 将自动化执行延迟指定的时间。

使用此操作可以在工作流程中插入延迟。您可以将延迟设置为特定持续时间,或直到达到特定时间。假设您正在运行多个 aws:runCommand 类型的 Run Command 步骤来配置实例,并且您希望确保它们之间有暂停。使用 aws:sleep,您可以插入延迟。

以下示例展示了如何使用持续时间或时间戳来定义 sleep 间隔 - 两者的格式均遵循 ISO 8601。

使用 JSON

{  
"name":"sleep",
"action":"aws:sleep",
"inputs":{
"Duration":"PT10M"
}
}

作为参数传入的持续时间:

{  
"name":"sleep",
"action":"aws:sleep",
"inputs":{
"Duration":"PT{{delayInMinutes}}M"
}
}

使用时间戳来终止 sleep 间隔:

{  
"name":"sleep",
"action":"aws:sleep",
"inputs":{
"Timestamp":"2017-05-30T01:00:00Z"
}
}

使用 YAML

- name: "sleep"
action: "aws:sleep"
inputs:
Duration: "PT10M"

作为参数传入的持续时间:

- name: "sleep"
action: "aws:sleep"
inputs:
Duration: "PT{{ delayInMinutes }}M"

使用时间戳来终止 sleep 间隔:

- name: "sleep"
action: "aws:sleep"
inputs:
Timestamp: "2017-05-30T01:00:00Z"

关于amazon-web-services - AWS Systems Manager 如何在自动化步骤之间添加延迟,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44253011/

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