gpt4 book ai didi

amazon-web-services - 基于 ELB 中的服务中主机的 CloudFormation 等待条件

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

是否有人有以下示例(最好是 Windows),

目前,我有一个 CF 模板,它在 ELB 下的健康主机上创建了我的 AutoScaling 组、LoadBalancer 和一些警报,并且正在正常工作,

我想要做的是在创建警报之前有一个等待条件,以便只有在主机被标记为“正在服务”但 ELB 时才创建警报。

我知道如何让等待条件发挥作用,但是我不知道如何根据 ELB 下的“服务中”状态使其发挥作用。

有人有任何建议或例子吗?我一直在网上寻找一些示例,但除了常见的 AWS 文档页面和其他一些页面之外,我无法找到我需要的内容。

我确实找到了一个关于如何验证实例运行状况的片段 - 但我无法弄清楚是否或如何将其纳入等待条件。

verify_instance_health:
commands:
ELBHealthCheck:
command: !Sub
'until ; do state=$(aws --region ${AWS::Region} elb describe-instance-health
--load-balancer-name ${ElasticLoadBalancer}
--instances $(curl -s http://169.254.169.254/latest/meta-data/instance-id)
--query InstanceStates[0].State); sleep 10; done'

干杯

最佳答案

您可以使用 Custom Resource 来完成此操作使用 DescribeInstanceHealth 监视实例API,当 EC2 实例达到 InService 状态时完成(例如,使用 instanceInService waiter 中的 AWS SDK for JavaScript )。

这是一个完整的示例,它使用 cfn-init 在 EC2 实例上配置 nginx,在 TCP 端口 80 上添加 ELB 运行状况检查,以及自定义资源 InService 等待 EC2 实例在 ELB 中InService 完成堆栈:

Launch Stack

Description: Wait until instance enters the InService state.
Parameters:
ImageId:
Description: Image ID to launch EC2 instances.
Type: AWS::EC2::Image::Id
# amzn-ami-hvm-2016.09.1.20161221-x86_64-gp2
Default: ami-9be6f38c
InstanceType:
Description: Instance type to launch EC2 instances.
Type: String
Default: m3.medium
AllowedValues: [ m3.medium, m3.large, m3.xlarge, m3.2xlarge ]
AvailabilityZones:
Description: Availability Zones for ELB.
Type: List<AWS::EC2::AvailabilityZone::Name>
Resources:
InstanceSecurityGroup:
Type: AWS::EC2::SecurityGroup
Properties:
GroupDescription: Allow inbound traffic from Load Balancer
SecurityGroupIngress:
Type: AWS::EC2::SecurityGroupIngress
Properties:
IpProtocol: tcp
FromPort: 80
ToPort: 80
GroupName: !Ref InstanceSecurityGroup
SourceSecurityGroupName: !GetAtt LoadBalancer.SourceSecurityGroup.GroupName
SourceSecurityGroupOwnerId: !GetAtt LoadBalancer.SourceSecurityGroup.OwnerAlias
Instance:
Type: AWS::EC2::Instance
Properties:
ImageId: !Ref ImageId
InstanceType: !Ref InstanceType
SecurityGroups: [!Ref InstanceSecurityGroup]
UserData:
"Fn::Base64": !Sub |
#!/bin/bash
/opt/aws/bin/cfn-init -v \
--stack ${AWS::StackName} \
--region ${AWS::Region} \
--resource Instance
Metadata:
AWS::CloudFormation::Init:
config:
packages: {yum: {nginx: []}}
services:
sysvinit:
nginx:
enabled: true
ensureRunning: true
files: [/etc/nginx/nginx.conf]
sources:
- /usr/share/nginx/html
- /etc/nginx/conf.d
- /etc/nginx/default.d
LoadBalancer:
Type: AWS::ElasticLoadBalancing::LoadBalancer
Properties:
AvailabilityZones: !Ref AvailabilityZones
Listeners:
- LoadBalancerPort: 80
InstancePort: 80
Protocol: HTTP
Instances: [!Ref Instance]
HealthCheck:
Target: TCP:80
HealthyThreshold: 2
UnhealthyThreshold: 5
Interval: 5
Timeout: 2
InService:
Type: Custom::InService
Properties:
ServiceToken: !GetAtt InServiceFunction.Arn
Instances:
- InstanceId: !Ref Instance
LoadBalancerName: !Ref LoadBalancer
InServiceFunction:
Type: AWS::Lambda::Function
Properties:
Handler: index.handler
Role: !GetAtt LambdaExecutionRole.Arn
Code:
ZipFile: !Sub |
var response = require('cfn-response');
var AWS = require('aws-sdk');
exports.handler = (event, context) => {
console.log("Request received:\n", JSON.stringify(event));
var physicalId = event.PhysicalResourceId || 'none';
var success = data => response.send(event, context, response.SUCCESS, data, physicalId);
var failed = e => response.send(event, context, response.FAILED, e, physicalId);
if (event.RequestType == 'Create') {
var elb = new AWS.ELB();
var elbParams = event.ResourceProperties;
delete elbParams.ServiceToken;
elb.waitFor('instanceInService', elbParams).promise().
then((data)=> success({}), (e)=> failed(e));
} else {
success({});
}
};
Runtime: nodejs4.3
Timeout: 300
LambdaExecutionRole:
Type: AWS::IAM::Role
Properties:
AssumeRolePolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: Allow
Principal: {Service: [lambda.amazonaws.com]}
Action: ['sts:AssumeRole']
Path: /
ManagedPolicyArns:
- arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole
Policies:
- PolicyName: ELBPolicy
PolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: Allow
Action:
- 'elasticloadbalancing:DescribeInstanceHealth'
Resource: ['*']
Outputs:
URL:
Value: !Sub "http://${LoadBalancer.DNSName}"

关于amazon-web-services - 基于 ELB 中的服务中主机的 CloudFormation 等待条件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42063936/

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