AWS ECS(Elastic Container Service)服务连接和服务发现是两种不同的方法,其中服务连接用于实时管理容器服务之间的通信连接,而服务发现则是一种自动化寻找容器服务的方式。
对于服务连接可使用如下代码示例:
1.创建一个VPC(Virtual Private Cloud)网络
2.创建容器服务并将其放置在该VPC网络中:
{
"AWSTemplateFormatVersion" : "2010-09-09",
"Description" : "Create a new VPC and launch an ECS cluster within it.",
"Resources" : {
"CompliantVPC" : {
"Type" : "AWS::EC2::VPC",
"Properties" : {
"CidrBlock" : "10.0.0.0/16",
"Tags": [
{"Key": "Name", "Value": "Compliant VPC"}
]
}
},
"CompliantSubnet" : {
"Type" : "AWS::EC2::Subnet",
"Properties" : {
"CidrBlock" : "10.0.1.0/24",
"VpcId" : { "Ref" : "CompliantVPC" },
"Tags": [
{"Key": "Name", "Value": "Compliant Subnet"}
]
}
},
"CompliantSecurityGroup" : {
"Type" : "AWS::EC2::SecurityGroup",
"Properties" : {
"GroupName" : "CompliantSecurityGroup",
"GroupDescription" : "Allow traffic to and from the EC2 instances and ELB in the Compliant VPC.",
"VpcId" : { "Ref" : "CompliantVPC" },
"SecurityGroupIngress" : [
{
"IpProtocol" : "tcp",
"FromPort" : "0",
"ToPort" : "65535",
"SourceSecurityGroupId" : { "Fn::GetAtt" : [ "CompliantSecurityGroup", "GroupId" ] }
}
]
}
},
"CompliantServiceRole" : {
"Type" : "AWS::IAM::Role",
"Properties" : {
"AssumeRolePolicyDocument": {
"Version" : "2012-10-17",
"Statement" : [ {
"Effect" : "Allow",
"Principal" : {"Service" : "ecs.amazonaws.com"},
"Action" : "sts:AssumeRole"
}
]
},
"Path" : "/",