【问题标题】:AWS Exporting DynamoDB Name to PythonAWS 将 DynamoDB 名称导出到 Python
【发布时间】:2019-07-13 05:24:48
【问题描述】:

我正在尝试创建一个 DynamoDB 表,但在 .yml 文件中没有 name 属性,以便它通过云形成命名,并将其名称导出到 python 以供访问,如果可以,我可以这样做吗?

我目前的想法是将名称导出为 ssm 参数,但我不确定如何。

【问题讨论】:

  • 您在单独的 template.yml 中有 DynamoDB 吗?还是通过控制台创建的?
  • @Deiv 目前是通过控制台创建的。我想把它转移到 yml
  • 知道了,一分钟后提交答案,很难通过评论格式化
  • 再澄清一点,您希望只在 yml 中引用 DB,还是您也在考虑通过 yml 创建它?因为后者会更好

标签: amazon-web-services amazon-dynamodb amazon-cloudformation aws-cli


【解决方案1】:

可以在cloudformation模板中打标签,在boto中通过标签获取资源。

import boto3

client = boto3.client('rds')

custom_filter = [{
    'Name':'tag:Owner', 
    'Values': ['user@example.com']}]

response = client.describe_instances(Filters=custom_filter)

(这段代码大部分抄自https://stackoverflow.com/a/48073016/10553976

这将对应于使用以下标记实例:

Tags: 
  - 
    Key: "Owner"
    Value: "user@example.com"

重要提示:如果要将标签应用于实例以外的其他对象,则需要使用与rds 客户端中的describe 方法不同的方法。

【讨论】:

  • 我认为我不能这样做。我目前正在做“boto3.resource('dynamodb',region).Table('TableNameFromConfigINI')”,有没有符合该代码的解决方案?
【解决方案2】:

我假设您的问题与 AWS CloudFormation 有关,因为您提到了 .yml 文件。

如果您的 python 函数在另一个模板中声明,您可以在 cloudformation 模板的输出部分报告名称。然后,您可以使用describe-stack API 或 CLI 从输出中获取值。

见: https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/outputs-section-structure.htmlhttps://docs.aws.amazon.com/cli/latest/reference/cloudformation/describe-stacks.html

如果你的python函数是在同一个模板中声明的,你可以参考你的逻辑资源来获取名字(根据https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-dynamodb-table.html

例如,

创建表的部分

MyTable:
    Type: AWS::DynamoDB::Table
    Description: your decsriptions
    Properties:
       ... your properties ...

引用它的部分(这里是AWS::AppSync::DataSource 的示例,但它适用于任何类型的资源

MyTableDataSource:
    Type: AWS::AppSync::DataSource 
    Properties:
      ...
      DynamoDBConfig:
        TableName:
          Ref: MyTable
        AwsRegion:
          Fn::Sub: ${AWS::Region}

或在 IAM 策略中获取表 ARN

  Policies:
  - PolicyName: mypolicy
    PolicyDocument:
      Version: 2012-10-17
      Statement:
      - Effect: Allow
        Action:
        - dynamodb:GetItem
        - dynamodb:PutItem
        - dynamodb:DeleteItem
        - dynamodb:UpdateItem
        - dynamodb:Query
        - dynamodb:Scan
        Resource:
        - Fn::Join:
          - ''
          - - Fn::GetAtt:
              - MyTable
              - Arn
            - '*'

【讨论】:

    【解决方案3】:

    根据您对我的评论的回复,您希望将 DynamoDB 表添加到 yml 文件中。如果表和 lambda 在同一个 .yml 中,那么您只需在 Lambda 环境变量中执行 !Ref YourTable。

    类似这样的:

    YourLambda:
      Type: AWS::Serverless::Function
      Properties:
        Environment:
          Variables:
            YourTableName: !Ref YourTable
    

    您还需要在属性 -> 策略下添加附加到 Lambda 的策略,并且您可以以相同的方式引用表名。

    但是,如果您希望在不移动 .yml 文件中的 DynamoDB 实例的情况下引用该名称,那么您必须通过在 Parameter Store 中创建一个条目来使其成为静态引用,然后像这样引用它(确保您的CFN 可以访问 ssm:getParameter):

    YourLambda:
      Type: AWS::Serverless::Function
      Properties:
        Environment:
          Variables:
            YourTableName: '{{resolve:ssm:/PATH/TO/TABLENAME:1}}'
    

    【讨论】:

      【解决方案4】:

      这是我最终的做法: 蟒蛇:

      ssm = boto3.client('ssm')
      resp = dict(ssm.get_parameter(Name='TableName', WithDecryption=False))
      tableName = str(json.loads(json.dumps(resp['Parameter'],default=str))['Value'])
      

      这是我的 cloudformation yaml 文件:

      Resources:
        DynamoTable:
          Type: "AWS::DynamoDB::Table"
            Properties:
              AttributeDefinitions:
                - AttributeName: A_Key
                  AttributeType: "S"
                - AttributeName: Serial
                  AttributeType: "S"
              KeySchema:
                - AttributeName: A_Key
                  KeyType: HASH
                - AttributeName: Serial
                  KeyType: RANGE
        DynamoTableParameter:
          Type: "AWS::SSM::Parameter”
          Properties:
            Name: "TableName”
            Type: String
            Value: !Ref DynamoTable
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-03-30
        • 2022-08-03
        • 1970-01-01
        • 1970-01-01
        • 2020-08-05
        • 1970-01-01
        相关资源
        最近更新 更多