我正在嘗試從我的 Fargate 任務訪問 DynamoDB,該任務是用 golang 編寫的。我得到的只是超時。我缺少什么?我使用 AWS 實驗室的 Cloudformation 模板以及允許完全 DynamoDB 訪問的任務角色。它是最簡單的公有子網模板加上 Fargate 模板。我嘗試添加 VPC 終端節點,但沒有什么區別。在我的機器上運行該任務是有效的。在本地和 AWS 上運行具有(或多或少)相同功能的 Python (Flask) 任務。這是相同的設置,我只是更改了任務圖像。這是代碼:package mainimport (? ? "context"? ? "fmt"? ? "github.com/aws/aws-sdk-go-v2/aws/endpoints"? ? "github.com/aws/aws-sdk-go-v2/aws/external"? ? "github.com/aws/aws-sdk-go-v2/service/dynamodb"? ? "github.com/gin-gonic/gin"? ? "time")var db *dynamodb.Clientfunc init() {? ? cfg, err := external.LoadDefaultAWSConfig()? ? if err != nil {? ? ? ? panic("unable to load SDK config, " + err.Error())? ? }? ? cfg.Region = endpoints.UsEast2RegionID? ? db = dynamodb.New(cfg)}func main() {? ? fmt.Println("go!")? ? router := gin.New()? ? router.Use(gin.Recovery())? ? router.GET("/ping", func(c *gin.Context) { c.JSON(200, gin.H{"msg": "pong"}) })? ? router.GET("/pong", func(c *gin.Context) {? ? ? ? req := db.ListTablesRequest(&dynamodb.ListTablesInput{})? ? ? ? ctx := context.Background()? ? ? ? ctx, cancelFn := context.WithTimeout(ctx, time.Second*5)? ? ? ? defer cancelFn()? ? ? ? res, err := req.Send(ctx)? ? ? ? if err != nil {? ? ? ? ? ? c.JSON(400, gin.H{"msg": "Fail", "error": err.Error()})? ? ? ? ? ? return? ? ? ? }? ? ? ? c.JSON(200, gin.H{"msg": fmt.Sprint(res)})? ? ? ? return? ? })? ? router.Run()}暫停:helles:v2> curl? xyz.us-east-2.elb.amazonaws.com/pong{"error":"RequestCanceled: request context canceled\ncaused by: context deadline exceeded","msg":"Fail"}預期的:helles:v2> curl 127.0.0.1:8080/pong{"msg":"{\n? TableNames: [\"OneTable\",\"OtherTable\"]\n}"}Python 進行比較:#!/usr/bin/env python3from flask import Flaskimport boto3dynamodb = boto3.client("dynamodb")app = Flask(__name__)@app.route("/ping")def index():? ? return "pong"@app.route("/pong")def pong():? ? return dynamodb.list_tables()if __name__ == "__main__":? ? app.run(debug=True, host="0.0.0.0", port=8080)結果有點不同,添加了元數據,但表名稱在那里。
在 Golang Fargate 任務中訪問 DynamoDB
慕田峪9158850
2023-07-26 17:27:28