英文:
How to retrieve ARN through AWS SDK for Go
问题
通过AWS SDK for Go,可以通过以下方式检索资源的ARN:
-
获取DynamoDB表的ARN:
使用DescribeTable
方法来获取表的详细信息,其中包括ARN。示例代码如下:import ( "github.com/aws/aws-sdk-go/aws" "github.com/aws/aws-sdk-go/aws/session" "github.com/aws/aws-sdk-go/service/dynamodb" ) func GetTableARN(tableName string) (string, error) { sess := session.Must(session.NewSessionWithOptions(session.Options{ SharedConfigState: session.SharedConfigEnable, })) svc := dynamodb.New(sess) input := &dynamodb.DescribeTableInput{ TableName: aws.String(tableName), } result, err := svc.DescribeTable(input) if err != nil { return "", err } return *result.Table.TableArn, nil }
通过调用
GetTableARN
函数并传入表名,将返回对应表的ARN。 -
获取
account-id
和region
:
使用AWS SDK for Go的session
包可以轻松地获取当前会话的账户ID和区域信息。示例代码如下:import ( "github.com/aws/aws-sdk-go/aws/session" ) func GetAccountIDAndRegion() (string, string) { sess := session.Must(session.NewSessionWithOptions(session.Options{ SharedConfigState: session.SharedConfigEnable, })) config := sess.Config return *config.Credentials.AccessKeyId, *config.Region }
通过调用
GetAccountIDAndRegion
函数,将返回当前会话的账户ID和区域信息。
英文:
Is there a way to retrieve ARN of a resource through AWS SDK for Go? I created a couple of tables in DynamoDB and I want to retrieve the ARNs.
The ARN format:
arn:aws:service:region:account-id:resource-type:resource-id
How to retrieve the account-id
and region
via SDK for Go?
答案1
得分: 1
没有通用的方法可以从AWS SDK中获取区域信息。在这里,通用指的是能够返回适用于任何环境中部署的服务的正确AWS区域的简单代码。
AWS假设相反的过程。作为客户端,您应该知道您的AWS资源部署在哪里,并且必须将区域注入到连接到AWS的应用程序中。
想象一下,您的代码在您的本地机器上运行,访问部署在us-east-2
区域的AWS DynamoDB,或者需要将数据从区域1的数据库复制到区域2的代码。在这两种情况下,应用程序无法在没有提示的情况下获取正确的区域。
然而,在许多情况下,部署代码的环境可以提供该提示。
以下是一些示例:
对于本地环境,您可以配置AWS SDK的默认区域-https://docs.aws.amazon.com/cli/latest/userguide/cli-configure-quickstart.html#cli-configure-quickstart-region。如果您使用config.LoadDefaultConfig创建客户端,您的服务将使用该区域。
另一个示例是在EC2上运行您的服务。EC2提供包含当前区域和帐户的AWS元数据。可以使用client.GetMetadata来请求当前区域。GetInstanceIdentityDocument API返回区域和帐户ID。
如果您控制服务的部署方式,您可以尝试从环境中获取当前区域和帐户ID,否则常见做法是在部署代码时设置带有区域和帐户ID的环境变量。
英文:
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 assumes the opposite process. 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.
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. 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. Your service picks up that region if you create client using config.LoadDefaultConfig
Another example is running your service on EC2. EC2 provides AWS metadata that includes current region and account. Current region can be requested using client.GetMetadata. GetInstanceIdentityDocument API returns both Region and Account 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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论