标签: 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万
查看次数

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
查看次数

是否可以在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
查看次数

将AWS CloudFormation转换为Terraform模板

我想将现有的AWS CloudFormation模板(yaml)自动转换为Terraform模板(hcl)。

任何人都可以建议一些工具,方法等吗?

amazon-web-services aws-cloudformation terraform

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

使用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
查看次数

如何在 cloudformation 策略文档中引用资源 ARN?(yaml)

我正在尝试在 cloudformation (yaml) 中定义角色和用户之间的信任关系策略文档。

为了在角色的 中指定用户的 ARN AssumeRolePolicyDocument,我想从实际的 cloudformation 资源中引用 ARN,而不必构造 ARN 字符串。

但是,它不起作用。当我使用 时!Ref rUser,在创建 cloudformation 堆栈时出现错误“策略中的无效主体”。

当我只是将 ARN 字符串粘贴为值时,它就起作用了。是因为!Ref rUser返回用户对象类型并且不计算为字符串吗?如果是这样,我如何从资源中引用 ARN?

代码:

  rUser:
    Type: "AWS::IAM::User"
    Properties:
      UserName: "my_user"

  rRole:
    DependsOn: rRole
    Type: "AWS::IAM::Role"
    Properties:
      RoleName: "my_role"
      AssumeRolePolicyDocument:
        Version: "2012-10-17"
        Statement:
          -
            Effect: "Allow"
            Principal:
              AWS:
                # this does not work, gives error "Invalid Principal in policy"
                - !Ref rUser
                # this does work (just hard coding the ARN string):
                # - "arn:aws:iam::111111111111:user/my_user"
            Action: …
Run Code Online (Sandbox Code Playgroud)

yaml amazon-web-services aws-cloudformation amazon-iam

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

CloudFormation 跨区域参考

当您在同一区域内运行多个 CloudFormation 堆栈时,您可以使用CloudFormation 输出跨堆栈共享引用

但是,正如该文档所强调的那样,输出不能用于跨区域引用。

您不能跨区域创建跨堆栈引用。您可以使用内部函数 Fn::ImportValue 仅导入已在同一区域内导出的值。

您如何在 CloudFormation 中跨区域引用值?

作为要遵循的示例,我有一个Route 53 托管区域部署在us-east-1. 但是,我有一个后端,us-west-2因为我想创建一个经过DNS 验证的 ACM 证书,该证书需要对托管区域的引用,以便能够创建适当的 CNAME 来证明所有权。

我将如何引用us-east-1从内部创建的托管区域 ID us-west-2

amazon-web-services aws-cloudformation aws-cloudformation-custom-resource

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

如何使用 CDK 获取资源的逻辑 ID?

我正在尝试为 CDK 构造编写一些测试,以验证定义为构造一部分的安全组规则。

构造看起来类似于以下内容。

export interface SampleConstructProps extends StackProps {
  srcSecurityGroupId: string;
}

export class SampleConstruct extends Construct {
  securityGroup: SecurityGroup;

  constructor(scope: Construct, id: string, props: SampleConstructProps) {
    super(scope, id, props);

    // const vpc = Vpc.fromLookup(...);
    this.securityGroup = new SecurityGroup(this, "SecurityGroup", {
      vpc: vpc,
      allowAllOutbound: true,
    });

    const srcSecurityGroupId = SecurityGroup.fromSecurityGroupId(stack, "SrcSecurityGroup", props.srcSecurityGroupId);

    this.securityGroup.addIngressRule(srcSecurityGroup, Port.tcp(22));
  }
}
Run Code Online (Sandbox Code Playgroud)

我想编写一个类似于以下内容的测试。

test("Security group config is correct", () => {
  const stack = new Stack();
  const srcSecurityGroupId = "id-123";
  const testConstruct = …
Run Code Online (Sandbox Code Playgroud)

amazon-web-services aws-cloudformation aws-cdk

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