首页 文章

如何在CloudFormation模板中为Elastic Beanstalk启动配置指定安全组?

提问于
浏览
3

我在CloudFormation模板中定义了以下安全组:

"APIInstanceSG": {
  "Type": "AWS::EC2::SecurityGroup",
  "Properties": {
    "GroupDescription": "Security Group for Application EC2 Instances,
    "VpcId": "vpc-10a75377",
    "Tags": [{
      "Key": "Name",
      "Value": "APIInstanceSG" }
    }]
  }
}

我还定义了一个Elastic Beanstalk环境,其中包含OptionSettings中的以下内容:

{
  "Namespace": "aws:autoscaling:launchconfiguration",
  "OptionName": "SecurityGroups",
  "Value": { "Ref": "APIInstanceSG" }
}

当我使用此模板创建堆栈时,安全组是在CloudFormation尝试创建EB环境之前创建的,但是当它尝试创建EB环境时,它会因以下错误而崩溃:

配置验证异常:无效的选项值:'sg-994fcbe4'(命名空间:'aws:autoscaling:launchconfiguration',OptionName:'SecurityGroups'):安全组'sg-994fcbe4'不存在

sg-994fcbe4是创建的安全组的ID
enter image description here

Elastic Beanstalk Environment配置如下:

"AspectAPIEnv": {
  "Type": "AWS::ElasticBeanstalk::Environment",
  "Properties": {
    "ApplicationName": "application-name",
    "EnvironmentName": "environment-name",
    "SolutionStackName": "64bit Amazon Linux 2016.09 v3.1.0 running Node.js",
    "Tier": {
      "Name": "WebServer",
      "Type": "Standard"
    },
    "OptionSettings": [
      {
        "Namespace": "aws:autoscaling:launchconfiguration",
        "OptionName": "EC2KeyName",
        "Value": "ec2-key"
      },
      {
        "Namespace": "aws:autoscaling:launchconfiguration",
        "OptionName": "IamInstanceProfile",
        "Value": "aws-elasticbeanstalk-ec2-role"
      },
      {
        "Namespace": "aws:autoscaling:launchconfiguration",
        "OptionName": "ImageId",
        "Value": "ami-d8356acf"
      },
      {
        "Namespace": "aws:autoscaling:launchconfiguration",
        "OptionName": "InstanceType",
        "Value": "t2.micro"
      },
      {
        "Namespace": "aws:autoscaling:launchconfiguration",
        "OptionName": "SecurityGroups",
        "Value": { "Ref": "APIInstanceSG" }
      },
      {
        "Namespace": "aws:autoscaling:trigger",
        "OptionName": "UpperThreshold",
        "Value": "6000000"
      },
      {
        "Namespace": "aws:autoscaling:updatepolicy:rollingupdate",
        "OptionName": "MaxBatchSize",
        "Value": "1"
      },
      {
        "Namespace": "aws:autoscaling:updatepolicy:rollingupdate",
        "OptionName": "MinInstancesInService",
        "Value": "1"
      },
      {
        "Namespace": "aws:autoscaling:updatepolicy:rollingupdate",
        "OptionName": "RollingUpdateEnabled",
        "Value": "true"
      },
      {
        "Namespace": "aws:autoscaling:updatepolicy:rollingupdate",
        "OptionName": "RollingUpdateType",
        "Value": "Health"
      },
      {
        "Namespace": "aws:elasticbeanstalk:command",
        "OptionName": "BatchSize",
        "Value": "30"
      },
      {
        "Namespace": "aws:elasticbeanstalk:container:nodejs",
        "OptionName": "NodeVersion",
        "Value": "6.2.2"
      },
      {
        "Namespace": "aws:elasticbeanstalk:environment",
        "OptionName": "ServiceRole",
        "Value": "aws-elasticbeanstalk-service-role"
      },
      {
        "Namespace": "aws:elasticbeanstalk:healthreporting:system",
        "OptionName": "SystemType",
        "Value": "enhanced"
      },
      {
        "Namespace": "aws:elasticbeanstalk:managedactions",
        "OptionName": "ManagedActionsEnabled",
        "Value": "true"
      },
      {
        "Namespace": "aws:elasticbeanstalk:managedactions",
        "OptionName": "PreferredStartTime",
        "Value": "SUN:09:02"
      },
      {
        "Namespace": "aws:elasticbeanstalk:managedactions:platformupdate",
        "OptionName": "UpdateLevel",
        "Value": "minor"
      },
      {
        "Namespace": "aws:elb:healthcheck",
        "OptionName": "Interval",
        "Value": "10"
      },
      {
        "Namespace": "aws:elb:loadbalancer",
        "OptionName": "CrossZone",
        "Value": "true"
      },
      {
        "Namespace": "aws:elb:loadbalancer",
        "OptionName": "LoadBalancerHTTPPort",
        "Value": "80"
      },
      {
        "Namespace": "aws:elb:loadbalancer",
        "OptionName": "SecurityGroups",
        "Value": { "Ref": "APILoadBalancerSG" }
      },
      {
        "Namespace": "aws:elb:loadbalancer",
        "OptionName": "ManagedSecurityGroup",
        "Value": { "Ref": "APILoadBalancerSG" }
      },
      {
        "Namespace": "aws:elb:policies",
        "OptionName": "ConnectionDrainingEnabled",
        "Value": "true"
      }
    ],
    "Tags": [
      {
        "Key": "Name",
        "Value": "AspectAPIEnv"
      }
    ]
  },
  "DependsOn": "RDSInstance"
}

4 回答

  • 0

    在查看您的AWS :: ElasticBeanstalk :: Environment资源后,我能够重现您遇到的错误 . 正如Marc Young在对您的问题的评论中建议的那样,您没有为您的环境指定VPC . 由于您的安全组位于VPC中,因此无法从同一VPC中的资源访问它 .

    要修复它,您必须将以下配置选项添加到您的环境:

    {
      "Namespace" : "aws:ec2:vpc",
      "OptionName" : "VPCId",
      "Value" : "vpc-10a75377"
    },
    

    如果指定VPC,则使用更新的模板创建堆栈将失败,并显示一条错误消息,指出您还需要指定环境子网,因此您必须添加以下选项:

    {
      "Namespace" : "aws:ec2:vpc",
      "OptionName" : "Subnets",
      "Value" : <insert the subnet for your instances here>
    },
    {
      "Namespace" : "aws:ec2:vpc",
      "OptionName" : "ELBSubnets",
      "Value" : <insert the subnet for your load balancer here>
    }
    

    您可以在Elastic Beanstalk CloudFormation sample templates中检查VPC中Beanstalk应用程序的工作示例 .

  • 0

    要克服这个:

    您需要从AWS CLI更改EB安全组,您无法从AWS Web Console进行更改 .

    考虑到您已经有AWS CLI installed ,如果要更改安全组,则需要执行此命令:

    aws elasticbeanstalk update-environment –environment-name –option-settings Namespace=aws:autoscaling:launchconfiguration,OptionName=SecurityGroups,Value=””
    

    Source

  • 0

    您应该在LC定义中设置DependsOn属性,以确保它在堆栈创建期间存在于SG之前 . 否则您无法保证参考将起作用 .

    "APIInstanceSG": {
      "Type": "AWS::EC2::SecurityGroup",
      "Properties": {
        "GroupDescription": "Security Group for Application EC2 Instances,
        "VpcId": "vpc-10a75377",
        "Tags": [{
          "Key": "Name",
          "Value": "APIInstanceSG" }
        }]
      },
      "DependsOn" : "APIInstanceSG"
    }
    

    http://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-attribute-dependson.html

  • 8

    在您的模板中,而不是

    "DependsOn" : "RDSInstance"
    

    写:

    "DependsOn": ["APIInstanceSG", "RDSInstance"]
    

    更多信息:http://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-attribute-dependson.html

相关问题