亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何在 AWS IAM 策略中引用 StringOutput

如何在 AWS IAM 策略中引用 StringOutput

Go
慕絲7291255 2022-08-15 15:45:24
我已經看了高高低低如何做到這一點。我不認為我有正確的術語。使用Pulumi和Golang如何引用字符串中某些資源的ID。例如,我創建了一個存儲桶,然后我想在 IAM 策略中引用該存儲桶的 ID。這似乎是不可能的。bucket, err := s3.NewBucket(    ctx,    photosBucketName,    &s3.BucketArgs{})tmpJSON, err := json.Marshal(map[string]interface{}{    "Version": "2012-10-17",    "Statement": []map[string]interface{}{        {            "Effect":    "Allow",            "Principal": "*",            "Action":    []string{"s3:GetObject"},            "Resource":  []string{fmt.Sprintf("arn:aws:s3:::%s/*", bucket.ID())},        },    },})輸出為:Sprintf format %s has arg bucket.ID() of wrong type github.com/pulumi/pulumi/sdk/v2/go/pulumi.IDOutput使用會導致文檔格式不正確,因為存儲桶名稱上生成的后綴。photosBucketName感謝您的時間和幫助。
查看完整描述

1 回答

?
Qyouu

TA貢獻1786條經驗 獲得超11個贊

Pulumi 資源返回 Outputs,這些值在上游云提供商 API (在本例中為 AWS S3 API) 創建資源之前,Pulumi 都不知道這些值。


這意味著,如果要以標準 Go 字符串的形式訪問原始輸出值,則需要以某種方式告訴 Pulumi 引擎等待該資源創建完畢。您可以使用Pulumi的應用程序執行此操作


因此,在您的特定示例中,我們希望為 IAM 策略構建一個 JSON 字符串(IAM 策略僅采用字符串,不能采用其他 Pulumi 輸出)。


bucket, err := s3.NewBucket(

    ctx,

    photosBucketName,

    &s3.BucketArgs{})


// notice how we're using the apply function to wrap the building of the JSON string

bucketPolicy := bucket.Arn.ApplyT(func (arn string) (string, error) {

    policyJSON, err := json.Marshal(map[string]interface{}{

        "Version": "2012-10-17",

        "Statement": []map[string]interface{}{

            {

                "Effect": "Allow",

                "Principal": "*",

                "Action": []string{"s3:GetObject"},

                "Resource": []string{

                    arn, // I can now pass the arn directy

                },

            },

        },

    })

    if err != nil {

        return "", err

    }

  return string(policyJSON), nil

})


查看完整回答
反對 回復 2022-08-15
  • 1 回答
  • 0 關注
  • 92 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號