标签: aws-cloudformation

Boto:检查CloudFormation堆栈是否存在的最佳方法是什么?

使用Boto检查CloudFormation堆栈是否存在且未处于损坏状态的最佳方法是什么?破碎我的意思是失败和回滚状态.

我不想使用try/except解决方案,因为boto将其记录为错误,并且在我的方案中,它将异常日志发送到警报系统.


目前我有以下解决方案:

1)使用 boto.cloudformation.connection.CloudFormationConnection.describe_stacks()

valid_states = '''\
CREATE_IN_PROGRESS
CREATE_COMPLETE
UPDATE_IN_PROGRESS
UPDATE_COMPLETE_CLEANUP_IN_PROGRESS
UPDATE_COMPLETE'''.splitlines()

def describe_stacks():
    result = []
    resp = cf_conn.describe_stacks()
    result.extend(resp)
    while resp.next_token:
        resp = cf_conn.describe_stacks(next_token=resp.next_token)
        result.extend(resp)
    return result


stacks = [stack for stack in describe_stacks() if stack.stack_name == STACK_NAME and stack.stack_status in valid_states]
exists = len(stacks) >= 1
Run Code Online (Sandbox Code Playgroud)

这很慢,因为我有很多堆栈.


2)使用 boto.cloudformation.connection.CloudFormationConnection.list_stacks()

def list_stacks(filters):
    result = []
    resp = cf_conn.list_stacks(filters)
    result.extend(resp)
    while resp.next_token:
        resp = cf_conn.list_stacks(filters, next_token=resp.next_token)
        result.extend(resp)
    return result

stacks = [stack for stack in list_stacks(valid_states) …
Run Code Online (Sandbox Code Playgroud)

python boto amazon-web-services aws-cloudformation

10
推荐指数
2
解决办法
1万
查看次数

尝试将参数从Master传递到子模板

我正在尝试将列表参数从主模板传递到子模板,但是我遇到了两个错误.这些是我在主模板上的当前参数.

"Parameters": {
    "ELBSubnets": {
        "Default": "subnet-5d8fea67,subnet-3e35cf15",
        "Type": "CommaDelimitedList"
    },
    "LCKeyPair": {
        "Default": "key-master",
        "Type": "String"
    },
    "LCSecurityGroups": {
        "Default": "sg-10a15c74,sg-880e5fec",
        "Type": "CommaDelimitedList"
    }
},
Run Code Online (Sandbox Code Playgroud)

传递给子模板时,它们在同一模板上的此方法中被引用.

    "ChildTempate1": {
        "Properties": {
            "Parameters": {
                "ELBSubnets": {
                    "Ref": "ELBSubnets"
                },
                "KeyPair": {
                    "Ref": "LCKeyPair"
                },
                "LCSecurityGroups": {
                    "Ref": "LCSecurityGroups"
                }
            },
Run Code Online (Sandbox Code Playgroud)

在子模板上,它们被声明完全相同.

"Parameters": {
    "ELBSubnets": {
        "Type": "CommaDelimitedList"
    },
    "LCKeyPair": {
        "Type": "String"
    },
    "LCSecurityGroups": {
        "Type": "CommaDelimitedList"
    }
},
Run Code Online (Sandbox Code Playgroud)

并且它们在子模板中的此方法中被引用.

            "KeyName": {
                "Ref": "LCKeyPair"
            },
            "SecurityGroups": {
                "Fn::Join": [
                    ",",
                    [
                        { …
Run Code Online (Sandbox Code Playgroud)

json amazon-web-services aws-cloudformation troposphere

10
推荐指数
2
解决办法
8688
查看次数

cfn-init比userdata有什么好处?

我的CloudFormation模板已经很长了.一个原因是因为我的AWS::CloudFormation::Init部分变得非常庞大.这是我所拥有的一个非常小的样本:

"ConfigDisk": {
    "commands": {
        "01formatFS": {
            "command": "/sbin/mkfs.ext4 /dev/xvdf"
        },
        "02mountFS": {
            "command": "/bin/mount /dev/xvdf /var/lib/jenkins"
        },
        "03changePerms": {
            "command": "/bin/chown jenkins:jenkins /var/lib/jenkins"
        },
        "04updateFStab": {
            "command": "/bin/echo /dev/xvdf /var/lib/jenkins ext4 defaults 1 1 >> /etc/fstab"
        }
    }
},
Run Code Online (Sandbox Code Playgroud)

将它作为一堆命令放入userdata部分不是更好吗?

/sbin/mkfs.ext4 /dev/xvdf
/bin/mount /dev/xvdf /var/lib/jenkins
/bin/chown jenkins:jenkins /var/lib/jenkins
/bin/echo /dev/xvdf /var/lib/jenkins ext4 defaults 1 1 >> /etc/fstab
Run Code Online (Sandbox Code Playgroud)

将此保留在Init over userdata中有什么好处?

amazon-web-services aws-cloudformation aws-sdk

10
推荐指数
3
解决办法
3533
查看次数

否定CloudFormation模板中的条件

我有以下条件,因此我创建了一些资源,而如果不满足那个条件,那么我创建其他资源.

Conditions:
  ISProduction:
    "Fn::Equals":
      - !Ref Environment
      - staging
  ISNotProduction:
      "Fn::Not":
        - !Ref ISProduction
Run Code Online (Sandbox Code Playgroud)

但是,当我尝试使用上面的代码段评估模板时,我收到错误:

模板错误:每个Fn :: Not对象都需要一个布尔参数

如何否定云形成模板中的条件?或者我如何使用ISProduction的否定?

我也在资源创建中尝试了下面的条件,但是我但模板没有通过验证,因为"每个条件成员必须是一个字符串".

Condition:
      "Fn::Not":
        - !Ref ISProduction
Run Code Online (Sandbox Code Playgroud)

cloud amazon-web-services aws-cloudformation

10
推荐指数
1
解决办法
7489
查看次数

是否可以在Terraform中执行CloudFormation文件?

一个团队已经将一个cloudformation模板编写为一个.yml提供一堆资源的文件.

是否可以通过在Terraform中执行来利用此文件?还是必须重写?

我是terraform的新手,刚入门.

如果我使用AWS CLI,我会执行这样的命令,

aws cloudformation create-stack --stack-name my-new-stack --template-body file://mystack.yml --parameters ParameterKey = AmiId

我想在我的terraform配置中包含相当于此命令的内容.

如果有可能,你可以给我一个例子,我真的很感激.

谢谢!

amazon-web-services aws-cloudformation terraform

10
推荐指数
2
解决办法
5494
查看次数

如何在CloudFormation中使用CodeBuild的输出工件?

所以我有一个相当简单的堆栈我正在尝试设置由一个订阅SNS主题的Lambda函数组成.我想使用CodePipeline有三个阶段:Source(GitHub) - > Build(CodeBuild) - > Deploy(CloudFormation).

我设法凑齐了一个模板和buildspec文件,这是有效的,除了我失去了我应该如何引用CodeBuild在CloudFormation模板中产生的输出工件; 现在我只有占位符内联代码.

基本上,Code:为了获得CodeBuild文件(这是我在CodePipeline中的输出工件),我应该放在Lambda函数的属性中?

template.yml:

AWSTemplateFormatVersion: 2010-09-09
Resources:
  SNSTopic:
    Type: 'AWS::SNS::Topic'
    Properties:
      Subscription:
        - Endpoint: !GetAtt
            - LambdaFunction
            - Arn
          Protocol: lambda
  LambdaFunction:
    Type: 'AWS::Lambda::Function'
    Properties:
      Runtime: python3.6
      Handler: main.lamda_handler
      Timeout: '10'
      Role: !GetAtt
        - LambdaExecutionRole
        - Arn
      Code:
        ZipFile: >
          def lambda_handler(event, context):
            print(event)
            return 'Hello, world!'
  LambdaExecutionRole:
    Type: 'AWS::IAM::Role'
    Properties:
      AssumeRolePolicyDocument:
        Version: 2012-10-17
        Statement:
          - Effect: Allow
            Principal:
              Service:
                - lambda.amazonaws.com
            Action:
              - 'sts:AssumeRole'
      ManagedPolicyArns:
        - 'arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole'
  LambdaInvokePermission:
    Type: 'AWS::Lambda::Permission' …
Run Code Online (Sandbox Code Playgroud)

amazon-web-services aws-cloudformation aws-codepipeline aws-codebuild

10
推荐指数
1
解决办法
3835
查看次数

模板格式错误:未解决的资源依赖关系

我尝试使用以下模板创建一个 EC2 实例:

Parameters:
  KeyName:
    Default: TestKeyPair
    Description: Name of an existing EC2 KeyPair to enable SSH access to the instance
    Type: AWS::EC2::KeyPair::KeyName
Resources:
  Dev:
    Properties:
      ImageId: ami-4e79ed36
      InstanceType: t2.micro
      KeyName: !Ref 'KeyName'
      SecurityGroups:
        - !Ref 'SSH'
    Type: AWS::EC2::Instance
Run Code Online (Sandbox Code Playgroud)

但我得到:

An error occurred (ValidationError) when calling the CreateChangeSet operation: Template format error: Unresolved resource dependencies [SSH] in the Resources block of the template
Run Code Online (Sandbox Code Playgroud)

我无法理解模板中有什么问题,因为名为“SSH”的安全组已经存在:

$ aws ec2 describe-security-groups --group-names SSH
....
"IpPermissions": [
    {
        "ToPort": 22,
        "IpRanges": [
            {
                "CidrIp": …
Run Code Online (Sandbox Code Playgroud)

amazon-web-services aws-cloudformation

10
推荐指数
1
解决办法
2万
查看次数

是否可以在 cloudformation 模板中静态指定 AWS::StackName?

是否可以在 cloudformation 模板中静态指定 AWS::StackName?还是只能在运行模板时将其指定为参数?

据我了解,这个值只能通过伪参数读取,不能设置:

https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/pseudo-parameter-reference.html#cfn-pseudo-param-stackname

amazon-web-services aws-cloudformation

10
推荐指数
1
解决办法
4261
查看次数

使用Terraform/CloudFormation/Something有效地部署lambda(仅部署那些已更改的)

到目前为止,我一直在使用CloudFormation来部署我的lambda.我发现这个过程非常缓慢且效率低下 - 例如.它可能需要几分钟但如果只是部署那个1功能它应该只需几秒钟?大多数功能都没有改变,但我相信CloudFormation无法区分,无论如何都会部署一切.有没有办法可以更有效地做到这一点?比如查看已更改的内容并仅部署更改?

另一个好处是我可能会有更少的版本?

lambda continuous-integration aws-cloudformation terraform

10
推荐指数
1
解决办法
626
查看次数

S3 创建 CloudFormation 导致 400 Bad Request

使用 CloudFormation 创建 S3 存储桶时遇到此问题。我收到一个 400 Bad 的请求。如果有人可以提供帮助,将不胜感激。

aws cloudformation deploy --profile DEV --stack-name testBucket --template-file create_bucket.yml --region us-east-1 --capabilities CAPABILITY_IAM CAPABILITY_NAMED_IAM --parameter-overrides BucketName=myBucket

模板:

    AWSTemplateFormatVersion: 2010-09-09
    Parameters:
      BucketName:
        Description: Provisioned read throughput for each table
        Type: String
    Resources:
      MYBUCKET:
        Type: AWS::S3::Bucket
        Properties:
          BucketName: ${BucketName}
      MYBUCKETPOLICY:
        Type: AWS::S3::BucketPolicy
        Properties:
          Bucket: !Ref MYBUCKET
          PolicyDocument:
            Id: ReportPolicy
            Version: "2012-10-17"
            Statement:
              - Sid: ReportBucketPolicyDoc
                Effect: Allow
                Action: "s3:*"
                Principal:
                  AWS: !Join ['', ["arn:aws:iam::", !Ref "AWS::AccountId", ":root"]]
                Resource: !Join ['', ['arn:aws:s3:::', !Ref MYBUCKET, '/*']] …
Run Code Online (Sandbox Code Playgroud)

amazon-s3 aws-cloudformation

10
推荐指数
2
解决办法
3586
查看次数