简体   繁体   English

获取使用 AWS CDK 创建的 dynamo 表的名称

[英]Get the name of a dynamo table created with AWS CDK

Using the example from here .使用此处的示例。 I can create a table and lambda function that has access to the table.我可以创建一个表和 lambda function 可以访问该表。 But I need to know the name of table to use table.put to add records.但是我需要知道表名才能使用table.put添加记录。

Short term I am declaring the table name in the dynamo_table_props and then exporting it to the lambda as an environment variable.短期内,我在dynamo_table_props中声明表名,然后将其作为环境变量导出到 lambda。 However, this is against the best practices to declare names.但是,这违背了声明名称的最佳实践

I've tried to access the pattern properties , but cannot find the correct syntax.我试图访问模式属性,但找不到正确的语法。

What I have working is:我的工作是:

from aws_solutions_constructs.aws_iot_lambda_dynamodb import IotToLambdaToDynamoDB
from aws_cdk import (
    aws_iot as iot,
    aws_lambda as _lambda,
    Stack
)
from constructs import Construct

IotToLambdaToDynamoDB(self, 'test-iot-lambda-dynamodb-stack',
            lambda_function_props=_lambda.FunctionProps(
                code=_lambda.Code.from_asset('lambda'),
                runtime=_lambda.Runtime.PYTHON_3_9,
                handler='index.handler',
                environment={ 
                    'TABLE_NAME':'hard_coded_table_name'
                },
            ),
            iot_topic_rule_props=iot.CfnTopicRuleProps(
                topic_rule_payload=iot.CfnTopicRule.TopicRulePayloadProperty(
                    rule_disabled=False,
                    description="Processing of DTC messages from the AWS Connected Vehicle Solution.",
                    sql="SELECT * FROM 'connectedcar/dtc/#'",
                    actions=[]
                )
            ),
            dynamo_table_props=ddb.TableProps(
               partition_key={'name': 'id', 'type': ddb.AttributeType.STRING},
               table_name='hard_coded_table_name'))

 

Assigning a table name as a Lambdas environment variable is not against best practices.将表名指定为 Lambdas 环境变量并不违反最佳实践。

"Environment variable lookups inside constructs and stacks are a common anti-pattern." “构造和堆栈中的环境变量查找是一种常见的反模式。”

This is stating that the use of environment variables inside your CDK stack is an anti-pattern.这表明在 CDK 堆栈中使用环境变量是一种反模式。

You can most definitely use a Lambdas Environment Variable to store your table name.您绝对可以使用 Lambdas 环境变量来存储您的表名。 Another approach is to use Parameter Store and obtain your values at runtime from your Lambda function.另一种方法是使用Parameter Store并在运行时从 Lambda function 获取您的值。

I found the hint to the answer in this AWS Blog Post .我在这篇AWS 博客文章中找到了答案的提示。

When you call the function for the construct you have to assign it to a variable, in this example I used iotToLambdaToDynamo .当您为构造调用 function 时,您必须将其分配给一个变量,在这个例子中我使用iotToLambdaToDynamo Then, the last line of the below code extracts the table name from the construct and injects it into the lambda as a environmental variable as per lynkfox answer.然后,下面代码的最后一行从构造中提取表名,并将其作为 lynkfox 答案的环境变量注入到 lambda 中。

from aws_solutions_constructs.aws_iot_lambda_dynamodb import 

IotToLambdaToDynamoDB
from aws_cdk import (
    aws_iot as iot,
    aws_lambda as _lambda,
    Stack
)
from constructs import Construct

iotToLambdaToDynamo = IotToLambdaToDynamoDB(self, 'test-iot-lambda-dynamodb-stack',
            lambda_function_props=_lambda.FunctionProps(
                code=_lambda.Code.from_asset('lambda'),
                runtime=_lambda.Runtime.PYTHON_3_9,
                handler='index.handler',
            ),
            iot_topic_rule_props=iot.CfnTopicRuleProps(
                topic_rule_payload=iot.CfnTopicRule.TopicRulePayloadProperty(
                    rule_disabled=False,
                    description="Processing of DTC messages from the AWS Connected Vehicle Solution.",
                    sql="SELECT * FROM 'connectedcar/dtc/#'",
                    actions=[]
                )
            ),

iotToLambdaToDynamo.lambda_function.add_environment("TABLE_NAME", iotToLambdaToDymamo.dynamo_table.table_name)

The best practices you reference to "not declare names" is regarding the aspect of CDK where you can use a 'name prop' as you did.您提到的“不声明名称”的最佳实践是关于 CDK 的方面,您可以在其中像您一样使用“名称道具”。 This is only against best practices because it prevents your stack from being redeployable as a copy environment in the same account (and is difficult with some naming situations like s3, where the names must be globally unique)这仅违反最佳实践,因为它会阻止您的堆栈作为同一帐户中的副本环境重新部署(并且在某些命名情况下很困难,例如 s3,其中名称必须是全局唯一的)

usually, it's perfectly fine to get around this by using a variable+common name - ie table_name="${environment}-hard-coded-name" that you then either pass into the stack at deployment time (using a context flag or parameter store or the like to retrieve them when cdk synth runs)通常,通过使用变量+通用名称来解决这个问题非常好 - 即table_name="${environment}-hard-coded-name"然后你可以在部署时将其传递到堆栈中(使用上下文标志或参数store 之类的在cdk synth运行时检索它们)

Alternatively, if you dont care about human readable names, each Resource in cdk is a class object.或者,如果您不关心人类可读的名称,cdk 中的每个资源都是 class object。

As per the documentation you can reference the table name with yourTable.tableName根据文档,您可以使用yourTable.tableName引用表名

Ive never used the IotToLambdaToDynamoDB construct before, but a quick look seems to indicate you can access the table and the lambda directly after instantiation.我以前从未使用过 IotToLambdaToDynamoDB 构造,但快速浏览似乎表明您可以在实例化后直接访问表和 lambda。

which means you could do something like:这意味着你可以做类似的事情:

yourIot.function.addEnvironment("dynamoDbName", yourIot.Table.tableName)

(referencing function properties and iot pattern properties ) (参考function 属性物联网模式属性

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM