AWS最佳架构--完整版
以下代码为AWS云服务中CloudFormation的完整架构模板,编写人:Xiong,于2019年12月12日上传至本站。
{
"AWSTemplateFormatVersion": "2010-09-09",
"Resources": {
"vpc": {
"Type": "AWS::EC2::VPC",
"Properties": {
"CidrBlock": {
"Ref": "vpccidr"
},
"EnableDnsHostnames": "true",
"EnableDnsSupport": "true",
"Tags": [
{
"Key": "Name",
"Value": "vpc"
}
]
}
},
"pubnet1": {
"Type": "AWS::EC2::Subnet",
"Properties": {
"AvailabilityZone": {
"Fn::Select": [
"0",
{
"Fn::GetAZs": ""
}
]
},
"CidrBlock": {
"Ref": "pubnet1cidr"
},
"Tags": [
{
"Key": "Name",
"Value": "pubnet1"
}
],
"VpcId": {
"Ref": "vpc"
}
}
},
"pubnet2": {
"Type": "AWS::EC2::Subnet",
"Properties": {
"AvailabilityZone": {
"Fn::Select": [
"1",
{
"Fn::GetAZs": ""
}
]
},
"CidrBlock": {
"Ref": "pubnet2cidr"
},
"Tags": [
{
"Key": "Name",
"Value": "pubnet2"
}
],
"VpcId": {
"Ref": "vpc"
}
}
},
"webnet1": {
"Type": "AWS::EC2::Subnet",
"Properties": {
"AvailabilityZone": {
"Fn::Select": [
"0",
{
"Fn::GetAZs": ""
}
]
},
"CidrBlock": {
"Ref": "webnet1cidr"
},
"Tags": [
{
"Key": "Name",
"Value": "webnet1"
}
],
"VpcId": {
"Ref": "vpc"
}
}
},
"webnet2": {
"Type": "AWS::EC2::Subnet",
"Properties": {
"AvailabilityZone": {
"Fn::Select": [
"1",
{
"Fn::GetAZs": ""
}
]
},
"CidrBlock": {
"Ref": "webnet2cidr"
},
"Tags": [
{
"Key": "Name",
"Value": "webnet2"
}
],
"VpcId": {
"Ref": "vpc"
}
}
},
"dbnet1": {
"Type": "AWS::EC2::Subnet",
"Properties": {
"AvailabilityZone": {
"Fn::Select": [
"0",
{
"Fn::GetAZs": ""
}
]
},
"CidrBlock": {
"Ref": "dbnet1cidr"
},
"Tags": [
{
"Key": "Name",
"Value": "dbnet1"
}
],
"VpcId": {
"Ref": "vpc"
}
}
},
"dbnet2": {
"Type": "AWS::EC2::Subnet",
"Properties": {
"AvailabilityZone": {
"Fn::Select": [
"1",
{
"Fn::GetAZs": ""
}
]
},
"CidrBlock": {
"Ref": "dbnet2cidr"
},
"Tags": [
{
"Key": "Name",
"Value": "dbnet2"
}
],
"VpcId": {
"Ref": "vpc"
}
}
},
"table1": {
"Type": "AWS::EC2::RouteTable",
"Properties": {
"Tags": [
{
"Key": "Name",
"Value": "table1"
}
],
"VpcId": {
"Ref": "vpc"
}
}
},
"table2": {
"Type": "AWS::EC2::RouteTable",
"Properties": {
"Tags": [
{
"Key": "Name",
"Value": "table2"
}
],
"VpcId": {
"Ref": "vpc"
}
}
},
"table3": {
"Type": "AWS::EC2::RouteTable",
"Properties": {
"Tags": [
{
"Key": "Name",
"Value": "table3"
}
],
"VpcId": {
"Ref": "vpc"
}
}
},
"igw": {
"Type": "AWS::EC2::InternetGateway",
"Properties": {}
},
"igwvpc": {
"Type": "AWS::EC2::VPCGatewayAttachment",
"Properties": {
"InternetGatewayId": {
"Ref": "igw"
},
"VpcId": {
"Ref": "vpc"
}
}
},
"natgateway1": {
"Type": "AWS::EC2::NatGateway",
"Properties": {
"AllocationId": {
"Fn::GetAtt": [
"eip1",
"AllocationId"
]
},
"SubnetId": {
"Ref": "pubnet1"
}
}
},
"natgateway2": {
"Type": "AWS::EC2::NatGateway",
"Properties": {
"AllocationId": {
"Fn::GetAtt": [
"eip2",
"AllocationId"
]
},
"SubnetId": {
"Ref": "pubnet2"
}
}
},
"eip1": {
"Type": "AWS::EC2::EIP",
"Properties": {}
},
"eip2": {
"Type": "AWS::EC2::EIP",
"Properties": {}
},
"RouteTablepubnet1": {
"Type": "AWS::EC2::SubnetRouteTableAssociation",
"Properties": {
"SubnetId": {
"Ref": "pubnet1"
},
"RouteTableId": {
"Ref": "table1"
}
}
},
"RouteTablepubnet2": {
"Type": "AWS::EC2::SubnetRouteTableAssociation",
"Properties": {
"SubnetId": {
"Ref": "pubnet2"
},
"RouteTableId": {
"Ref": "table1"
}
}
},
"RouteTablewebnet3": {
"Type": "AWS::EC2::SubnetRouteTableAssociation",
"Properties": {
"SubnetId": {
"Ref": "webnet1"
},
"RouteTableId": {
"Ref": "table2"
}
}
},
"RouteTablewebnet4": {
"Type": "AWS::EC2::SubnetRouteTableAssociation",
"Properties": {
"SubnetId": {
"Ref": "webnet2"
},
"RouteTableId": {
"Ref": "table3"
}
}
},
"RouteTabledbnet5": {
"Type": "AWS::EC2::SubnetRouteTableAssociation",
"Properties": {
"SubnetId": {
"Ref": "dbnet1"
},
"RouteTableId": {
"Ref": "table2"
}
}
},
"RouteTabledbnet6": {
"Type": "AWS::EC2::SubnetRouteTableAssociation",
"Properties": {
"SubnetId": {
"Ref": "dbnet2"
},
"RouteTableId": {
"Ref": "table3"
}
}
},
"route1": {
"Type": "AWS::EC2::Route",
"Properties": {
"DestinationCidrBlock": "0.0.0.0/0",
"GatewayId": {
"Ref": "igw"
},
"RouteTableId": {
"Ref": "table1"
}
}
},
"route2": {
"Type": "AWS::EC2::Route",
"Properties": {
"DestinationCidrBlock": "0.0.0.0/0",
"NatGatewayId": {
"Ref": "natgateway1"
},
"RouteTableId": {
"Ref": "table2"
}
}
},
"route3": {
"Type": "AWS::EC2::Route",
"Properties": {
"DestinationCidrBlock": "0.0.0.0/0",
"NatGatewayId": {
"Ref": "natgateway2"
},
"RouteTableId": {
"Ref": "table3"
}
}
},
"pubgroup": {
"Type": "AWS::EC2::SecurityGroup",
"Properties": {
"GroupDescription": "pubgroup",
"GroupName": "pubgroup",
"Tags": [
{
"Key": "Name",
"Value": "pubgroup"
}
],
"SecurityGroupIngress": [
{
"CidrIp": "0.0.0.0/0",
"FromPort": {
"Ref":"pubfromport"
},
"IpProtocol": "tcp",
"ToPort": {
"Ref":"pubtoport"
}
}
],
"VpcId": {
"Ref": "vpc"
}
}
},
"webgroup": {
"Type": "AWS::EC2::SecurityGroup",
"Properties": {
"GroupDescription": "webgroup",
"GroupName": "webgroup",
"Tags": [
{
"Key": "Name",
"Value": "webgroup"
}
],
"SecurityGroupIngress": [
{
"SourceSecurityGroupId": {
"Ref": "pubgroup"
},
"FromPort": {
"Ref":"webfromport"
},
"IpProtocol": "tcp",
"ToPort": {
"Ref":"webtoport"
}
}
],
"VpcId": {
"Ref": "vpc"
}
}
},
"dbgroup": {
"Type": "AWS::EC2::SecurityGroup",
"Properties": {
"GroupName": "dbgroup",
"GroupDescription": "dbgroup",
"Tags": [
{
"Key": "Name",
"Value": "dbgroup"
}
],
"SecurityGroupIngress": [
{
"SourceSecurityGroupId": {
"Ref": "webgroup"
},
"FromPort": "3306",
"IpProtocol": "tcp",
"ToPort": "3306"
},
{
"SourceSecurityGroupId": {
"Ref": "webgroup"
},
"FromPort": {
"Ref": "cacheport"
},
"ToPort": {
"Ref": "cacheport"
},
"IpProtocol": "tcp"
}
],
"VpcId": {
"Ref": "vpc"
}
}
},
"pubserver1EC2": {
"Type": "AWS::EC2::Instance",
"Properties": {
"ImageId": {
"Ref": "aminame"
},
"InstanceType": "t3.micro",
"KeyName": {
"Ref":"keyname"
},
"Tags": [
{
"Key": "Name",
"Value": "pubserver1"
}
],
"NetworkInterfaces": [
{
"AssociatePublicIpAddress": "true",
"DeviceIndex": "0",
"GroupSet": [
{
"Ref": "pubgroup"
}
],
"SubnetId": {
"Ref": "pubnet1"
}
}
]
}
},
"pubserver2EC2": {
"Type": "AWS::EC2::Instance",
"Properties": {
"ImageId": {
"Ref": "aminame"
},
"InstanceType": "t3.micro",
"KeyName": {
"Ref":"keyname"
},
"Tags": [
{
"Key": "Name",
"Value": "pubserver2"
}
],
"NetworkInterfaces": [
{
"AssociatePublicIpAddress": "true",
"DeviceIndex": "0",
"GroupSet": [
{
"Ref": "pubgroup"
}
],
"SubnetId": {
"Ref": "pubnet2"
}
}
]
}
},
"listener": {
"Type": "AWS::ElasticLoadBalancingV2::Listener",
"Properties": {
"DefaultActions": [
{
"Type": "forward",
"TargetGroupArn": {
"Ref": "target"
}
}
],
"LoadBalancerArn": {
"Ref": "alb"
},
"Port": {
"Ref":"listenerport"
},
"Protocol": "HTTP"
}
},
"alb": {
"Type": "AWS::ElasticLoadBalancingV2::LoadBalancer",
"Properties": {
"Name": "alb",
"Subnets": [
{
"Ref": "pubnet1"
},
{
"Ref": "pubnet2"
}
],
"SecurityGroups": [
{
"Ref": "pubgroup"
}
]
}
},
"target": {
"Type": "AWS::ElasticLoadBalancingV2::TargetGroup",
"Properties": {
"Name": "target",
"Protocol": "HTTP",
"Port": {
"Ref":"listenerport"
},
"VpcId": {
"Ref": "vpc"
},
"HealthCheckProtocol": "HTTP",
"HealthCheckPath": "/",
"HealthCheckEnabled": "true",
"HealthCheckPort": {
"Ref":"listenerport"
},
"HealthyThresholdCount": "2",
"UnhealthyThresholdCount": "2",
"HealthCheckTimeoutSeconds": "5",
"HealthCheckIntervalSeconds": "6"
}
},
"asg1": {
"Type": "AWS::AutoScaling::AutoScalingGroup",
"Properties": {
"AutoScalingGroupName": "asg1",
"VPCZoneIdentifier": [
{
"Ref": "webnet1"
},
{
"Ref": "webnet2"
}
],
"MaxSize": "6",
"MinSize": "2",
"HealthCheckGracePeriod": "200",
"DesiredCapacity": "2",
"HealthCheckType": "ELB",
"TargetGroupARNs": [
{
"Ref": "target"
}
],
"LaunchConfigurationName": {
"Ref": "config"
}
}
},
"config": {
"Type": "AWS::AutoScaling::LaunchConfiguration",
"Properties": {
"ImageId": {
"Ref": "aminame"
},
"KeyName": {
"Ref":"keyname"
},
"SecurityGroups": [
{
"Ref": "webgroup"
}
],
"InstanceType": "t3.micro"
}
},
"cachegroup": {
"Type": "AWS::ElastiCache::SubnetGroup",
"Properties": {
"CacheSubnetGroupName": "cachegroup",
"Description": "cachegroup",
"SubnetIds": [
{
"Ref": "dbnet1"
},
{
"Ref": "dbnet2"
}
]
}
},
"cachecluster": {
"Type": "AWS::ElastiCache::CacheCluster",
"Properties": {
"Engine": "memcached",
"ClusterName": "cachecluster",
"NumCacheNodes": "2",
"Port": {
"Ref":"cacheport"
},
"CacheNodeType": "cache.t3.micro",
"VpcSecurityGroupIds": [
{
"Ref": "dbgroup"
}
],
"CacheSubnetGroupName": {
"Ref": "cachegroup"
}
}
},
"rdsgroup": {
"Type": "AWS::RDS::DBSubnetGroup",
"Properties": {
"DBSubnetGroupDescription": "rdsgroup",
"DBSubnetGroupName": "rdsgroup",
"SubnetIds": [
{
"Ref": "dbnet1"
},
{
"Ref": "dbnet2"
}
],
"Tags": [
{
"Key": "Name",
"Value": "rdsgroup"
}
]
}
},
"MyDB1": {
"Type": "AWS::RDS::DBInstance",
"Properties": {
"AllocatedStorage": "20",
"DBInstanceClass": "db.t2.micro",
"Engine": "MySQL",
"EngineVersion": "8.0.16",
"MasterUsername":"cstor",
"MultiAZ" : "true",
"MasterUserPassword":"123456789",
"Tags": [
{
"Key": "Name",
"Value": "My SQL Database1"
}
],
"DBSubnetGroupName":{
"Ref":"rdsgroup"
},
"VPCSecurityGroups": [
{
"Ref": "dbgroup"
}
]
}
}
},
"Parameters": {
"vpccidr": {
"Type": "String",
"Default": "10.1.0.0/16"
},
"pubnet1cidr": {
"Type": "String",
"Default": "10.1.1.0/24"
},
"pubnet2cidr": {
"Type": "String",
"Default": "10.1.2.0/24"
},
"webnet1cidr": {
"Type": "String",
"Default": "10.1.3.0/24"
},
"webnet2cidr": {
"Type": "String",
"Default": "10.1.4.0/24"
},
"dbnet1cidr": {
"Type": "String",
"Default": "10.1.5.0/24"
},
"dbnet2cidr": {
"Type": "String",
"Default": "10.1.6.0/24"
},
"cacheport": {
"Type": "String",
"Default": "11211"
},
"aminame": {
"Type": "String",
"Default": ""
},
"listenerport":{
"Type":"String",
"Default":"7777"
},
"pubfromport":{
"Type":"String",
"Default":"0"
},
"pubtoport":{
"Type":"String",
"Default":"65535"
},
"webfromport":{
"Type":"String",
"Default":"7777"
},
"webtoport":{
"Type":"String",
"Default":"7777"
},
"dbfromport":{
"Type":"String",
"Default":"3305"
},
"dbtoport":{
"Type":"String",
"Default":"3305"
},
"keyname":{
"Type":"String",
"Default":""
}
},
"Outputs": {
"pubnet1id": {
"Value": {
"Ref": "pubnet1"
}
},
"pubnet2id": {
"Value": {
"Ref": "pubnet2"
}
},
"webnet1id": {
"Value": {
"Ref": "webnet1"
}
},
"webnet2id": {
"Value": {
"Ref": "webnet2"
}
},
"dbnet1id": {
"Value": {
"Ref": "dbnet1"
}
},
"dbnet2id": {
"Value": {
"Ref": "dbnet2"
}
},
"vpcid": {
"Value": {
"Ref": "vpc"
}
},
"pubgroupid": {
"Value": {
"Ref": "pubgroup"
}
},
"webgroupid": {
"Value": {
"Ref": "webgroup"
}
},
"dbgroupid": {
"Value": {
"Ref": "dbgroup"
}
}
}
}