aws-cloudformation - CloudFormation模板: reduce boilerplate code

标签 aws-cloudformation boilerplate

我正在尝试编写 CF 模板来部署由多个 Lambda 组成的无服务器系统。就我而言,Lambda 资源描述共享许多属性;唯一的区别是文件名和处理函数。

如何在模板中定义诸如通用参数集之类的内容?

这个样板很糟糕:

  LambdaCreateUser:
    Type: AWS::Lambda::Function
    Properties: 
      Code:
        S3Bucket:
          Ref: BucketForLambdas
        S3Key: create_user.zip
      Handler: create_user.lambda_handler
      Runtime: python3.7
      Role: 
        Fn::GetAtt: [ LambdaRole , "Arn" ]
      Environment:
        Variables: { "EnvTable": !Ref EnvironmentTable, "UsersTable": !Ref UsersTable }
  LambdaDeleteUser:
    Type: AWS::Lambda::Function
    Properties: 
      Code:
        S3Bucket:
          Ref: BucketForLambdas
        S3Key: delete_user.zip 
      Handler: delete_user.lambda_handler  
      Runtime: python3.7   
      Role:
        Fn::GetAtt: [ LambdaRole , "Arn" ]
      Environment:
        Variables: { "EnvTable": !Ref EnvironmentTable, "UsersTable": !Ref UsersTable }

最佳答案

您正在寻找的是 AWS SAM它是 CloudFormation 之上的一层语法糖。使用 AWS SAM 的模板的基本表示如下所示:

AWSTemplateFormatVersion: '2010-09-09'
Transform: 'AWS::Serverless-2016-10-31'

Globals:
  Function:
    Runtime: python3.7
    Environment:
      Variables:
        EnvTable: !Ref EnvironmentTable
        UsersTable: !Ref UsersTable

Resources:
  LambdaCreateUser:
    Type: AWS::Serverless::Function
    Properties: 
      Code:
        S3Bucket:
          Ref: BucketForLambdas
        S3Key: create_user.zip
      Handler: create_user.lambda_handler
      Role: !GetAtt LambdaRole.Arn
  LambdaDeleteUser:
    Type: AWS::Serverless::Function
    Properties: 
      Code:
        S3Bucket:
          Ref: BucketForLambdas
        S3Key: delete_user.zip 
      Handler: delete_user.lambda_handler  
      Role: !GetAtt LambdaRole.Arn

但这还没有结束。您可以将代码定义替换为代码甚至内联代码的路径,并使用 sam buildsam package 来构建和上传您的工件。您也可以将角色定义替换为 SAM policy templates进一步减少样板代码。

关于aws-cloudformation - CloudFormation模板: reduce boilerplate code,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58138357/

相关文章:

amazon-web-services - 使用 CloudFormation 为 APIGateway 提供适当的子域

amazon-web-services - AWS EC2 机器没有足够的磁盘空间

amazon-web-services - 数据库实例和EC2安全组位于不同的VPC,cloudFormation错误

yaml - Fn::GetAZs + Fn::Select 不合作

sharepoint - 如何在 SharePoint 2010 母版页中使用 Paul Irish 的条件注释

boilerplate - javascript开头的双斜杠包括

javascript - AngularJS - 主模块无法识别子模块的状态

amazon-web-services - UI 中的 Cloudformation 模板

java - 如何在多个 JTextfield 上调用 `setText`?

php - PHP中缺少对象属性如何解决?