简体   繁体   English

如何通过 AWS SDK 为 Go 检索 ARN

[英]How to retrieve ARN through AWS SDK for Go

Is there a way to retrieve ARN of a resource through AWS SDK for Go?有没有办法通过 AWS SDK 为 Go 检索资源的 ARN? I created a couple of tables in DynamoDB and I want to retrieve the ARNs.我在 DynamoDB 中创建了几个表,我想检索 ARN。

The ARN format: arn:aws:service:region:account-id:resource-type:resource-id ARN 格式: arn:aws:service:region:account-id:resource-type:resource-id

How to retrieve the account-id and region via SDK for Go?如何通过SDK获取Go的account-idregion

There is no generic way to get region from AWS SDK. By generic, here we consider simple code that returns a correct AWS region for your service deployed to ANY environment.没有从 AWS SDK 获取区域的通用方法。通过通用,我们在这里考虑简单的代码,为部署到任何环境的服务返回正确的 AWS 区域。

AWS assumes the opposite process. AWS 假设相反的过程。 As a client, you are expected to know where your AWS resources deployed, and you have to inject region into an app that connects to AWS.作为客户,您应该知道您的 AWS 资源部署在哪里,并且您必须将区域注入到连接到 AWS 的应用程序中。

Think about your code running on your local machine in Europe, accessing AWS DynamoDB deployed in us-east-2 region, or code that needs to copy data from DB in region1 to DB in region2.想想你在欧洲本地机器上运行的代码,访问部署在us-east-2区域的 AWS DynamoDB,或者需要将数据从 region1 的 DB 复制到 region2 的 DB 的代码。 In both these cases, the application cannot get the correct region without a hint.在这两种情况下,应用程序都无法在没有提示的情况下获得正确的区域。

In many cases, though, the environment where your code is deployed can provide that hint.但是,在许多情况下,部署代码的环境可以提供该提示。

A few examples:几个例子:

For local environment, you can configure default region for AWS SDK - https://docs.aws.amazon.com/cli/latest/userguide/cli-configure-quickstart.html#cli-configure-quickstart-region .对于本地环境,您可以为 AWS SDK - https://docs.aws.amazon.com/cli/latest/userguide/cli-configure-quickstart.html#cli-configure-quickstart-region配置默认区域。 Your service picks up that region if you create client using config.LoadDefaultConfig如果您使用config.LoadDefaultConfig创建客户端,您的服务将选择该区域

Another example is running your service on EC2.另一个例子是在 EC2 上运行您的服务。 EC2 provides AWS metadata that includes current region and account. EC2 提供包括当前区域和账户的 AWS 元数据。 Current region can be requested using client.GetMetadata .可以使用client.GetMetadata请求当前区域。 GetInstanceIdentityDocument API returns both Region and Account ID. GetInstanceIdentityDocument API 返回区域和帐户 ID。

If you control how your service is deployed, you can try to get current Region and Account ID from environment, otherwise common practice is setting ENV variables with Region and Account ID when you deploy your code.如果您控制服务的部署方式,则可以尝试从环境中获取当前区域和帐户 ID,否则通常的做法是在部署代码时使用区域和帐户 ID 设置 ENV 变量。

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

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