在 Golang Fargate 任務(wù)中訪問 DynamoDB
我正在嘗試從我的 Fargate 任務(wù)訪問 DynamoDB,該任務(wù)是用 golang 編寫的。我得到的只是超時(shí)。我缺少什么?我使用 AWS 實(shí)驗(yàn)室的 Cloudformation 模板以及允許完全 DynamoDB 訪問的任務(wù)角色。它是最簡單的公有子網(wǎng)模板加上 Fargate 模板。我嘗試添加 VPC 終端節(jié)點(diǎn),但沒有什么區(qū)別。在我的機(jī)器上運(yùn)行該任務(wù)是有效的。在本地和 AWS 上運(yùn)行具有(或多或少)相同功能的 Python (Flask) 任務(wù)。這是相同的設(shè)置,我只是更改了任務(wù)圖像。這是代碼: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"}預(yù)期的:helles:v2> curl 127.0.0.1:8080/pong{"msg":"{\n? TableNames: [\"OneTable\",\"OtherTable\"]\n}"}Python 進(jìn)行比較:#!/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)結(jié)果有點(diǎn)不同,添加了元數(shù)據(jù),但表名稱在那里。
查看完整描述