简体   繁体   English

(cdk + boto3):在 CDK 应用程序中使用 boto3

[英](cdk + boto3): Use boto3 inside CDK application

I run CDK against different environments using ENV=<enviornment name> cdk synth --profile <PROFILE_NAME> --all - now I want to create SSM secret parameter inside my CDK application.我使用ENV=<enviornment name> cdk synth --profile <PROFILE_NAME> --all针对不同的环境运行 CDK - 现在我想在我的 CDK 应用程序中创建 SSM 秘密参数。 This should only happen during scaffolding but needs boto3 as there is no CloudFormation equivalent.这应该只发生在脚手架期间,但需要 boto3,因为没有等效的 CloudFormation。 How can I reuse the CDK authentication together with boto3?如何将 CDK 身份验证与 boto3 一起重用? I develop in Python both with the CDK as with boto3 and use an MFA.我在 Python 中开发了 CDK 和 boto3,并使用了 MFA。 Any ideas?有任何想法吗?

#1 Trial - Using cached credentials #1 试用 - 使用缓存凭证

Not working as CDK is not caching credentials:(不工作,因为 CDK 不缓存凭据:(

I came a accross some way to retrive cached credentials with boto3.我遇到了一些使用 boto3 检索缓存凭据的方法。 Still I need the profile used with CDK, as without I get the error message botocore.exceptions.NoCredentialsError: Unable to locate credentials .我仍然需要与 CDK 一起使用的配置文件,因为如果没有,我会收到错误消息botocore.exceptions.NoCredentialsError: Unable to locate credentials Does somebody know how to get the AWS CDK profile used within the code?有人知道如何获取代码中使用的 AWS CDK 配置文件吗? Maybe the first step to a solution.也许是解决方案的第一步。

# By default the cache path is ~/.aws/boto/cache
cli_cache = os.path.join(os.path.expanduser("~"), ".aws/cli/cache")

# Construct botocore session with cache
session = botocore.session.get_session()
session.get_component("credential_provider").get_provider(
    "assume-role"
).cache = credentials.JSONFileCache(cli_cache)

# Create boto3 client from session
return boto3.Session(botocore_session=session)

I would suggest looking into custom resources , it is a way to call AWS api's (or even execute any code) inside of CDK.我建议查看自定义资源,这是一种在 CDK 内部调用 AWS api(甚至执行任何代码)的方法。

Another way of doing this is using Lambda. In this example , you can see how tables were created after provisioning RDS.另一种方法是使用 Lambda。在此示例中,您可以看到在配置 RDS 后如何创建表。 In your case you can replace table creation with SSM secret parameter creation.在您的情况下,您可以用 SSM 秘密参数创建替换表创建。

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

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