第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

如何在 go 中編寫前/后流量鉤子函數(shù)?

如何在 go 中編寫前/后流量鉤子函數(shù)?

Go
三國紛爭(zhēng) 2023-05-22 17:12:37
我開始使用 AWS SAM,現(xiàn)在我只有一些單元測(cè)試,但我想嘗試在預(yù)流量掛鉤函數(shù)中運(yùn)行集成測(cè)試。不幸的是,似乎沒有 Golang 的代碼示例,我只能找到 Javascript 的代碼示例。我想從測(cè)試一個(gè)簡(jiǎn)單查詢 DynamoDB 的 lambda 函數(shù)開始。
查看完整描述

2 回答

?
慕斯709654

TA貢獻(xiàn)1840條經(jīng)驗(yàn) 獲得超5個(gè)贊

這樣的事情有效:


package main


import (

    "context"

    "encoding/json"


    "github.com/aws/aws-lambda-go/lambda"

    "github.com/aws/aws-sdk-go/aws/session"

    "github.com/aws/aws-sdk-go/service/codedeploy"

)


type CodeDeployEvent struct {

    DeploymentId                  string `json:"deploymentId"`

    LifecycleEventHookExecutionId string `json:"lifecycleEventHookExecutionId"`

}


func HandleRequest(ctx context.Context, event CodeDeployEvent) (string, error) {


    // add some tests here and change status flag as needed . . .


    client := codedeploy.New(session.New())

    params := &codedeploy.PutLifecycleEventHookExecutionStatusInput{

        DeploymentId:                  &event.DeploymentId,

        LifecycleEventHookExecutionId: &event.LifecycleEventHookExecutionId,

        Status:                        "Succeeded",

    }


    req, _ := client.PutLifecycleEventHookExecutionStatusRequest(params)


    _ = req.Send()

}


查看完整回答
反對(duì) 回復(fù) 2023-05-22
?
12345678_0001

TA貢獻(xiàn)1802條經(jīng)驗(yàn) 獲得超5個(gè)贊

我開始實(shí)施這個(gè)并想分享我的完整解決方案。

在弄清楚如何使用它之后,我決定不使用它,因?yàn)樗袔讉€(gè)缺點(diǎn)。

  • 無法將新版本的金絲雀公開給用戶群的專用部分,這意味著有時(shí)他們會(huì)使用新版本或舊版本

  • 調(diào)用發(fā)布到 sns 的函數(shù)會(huì)觸發(fā)所有下游動(dòng)作,這可能會(huì)獲取下游服務(wù)的新版本或舊版本,這會(huì)在破壞 API 的情況下導(dǎo)致很多問題

  • IAM 更改會(huì)立即影響兩個(gè)版本,可能會(huì)破壞舊版本。

相反,我將所有內(nèi)容部署到預(yù)生產(chǎn)帳戶,運(yùn)行我的集成和端到端測(cè)試,如果它們成功,我將部署到生產(chǎn)

創(chuàng)建金絲雀部署的 cdk 代碼:

const versionAlias = new lambda.Alias(this, 'Alias', {

    aliasName: "alias",

    version: this.lambda.currentVersion,

})


const preHook = new lambda.Function(this, 'LambdaPreHook', {

    description: "pre hook",

    code: lambda.Code.fromAsset('dist/upload/convert-pre-hook'),

    handler: 'main',

    runtime: lambda.Runtime.GO_1_X,

    memorySize: 128,

    timeout: cdk.Duration.minutes(1),

    environment: {

        FUNCTION_NAME: this.lambda.currentVersion.functionName,

    },

    reservedConcurrentExecutions: 5,

    logRetention: RetentionDays.ONE_WEEK,

})

// this.lambda.grantInvoke(preHook) // this doesn't work, I need to grant invoke to all functions :s

preHook.addToRolePolicy(new iam.PolicyStatement({

    actions: [

        "lambda:InvokeFunction",

    ],

    resources: ["*"],

    effect: iam.Effect.ALLOW,

}))


const application = new codedeploy.LambdaApplication(this, 'CodeDeployApplication')

new codedeploy.LambdaDeploymentGroup(this, 'CanaryDeployment', {

    application: application,

    alias: versionAlias,

    deploymentConfig: codedeploy.LambdaDeploymentConfig.ALL_AT_ONCE,

    preHook: preHook,

    autoRollback: {

        failedDeployment: true,

        stoppedDeployment: true,

        deploymentInAlarm: false,

    },

    ignorePollAlarmsFailure: false,

    // alarms:

    // autoRollback: codedeploy.A

    // postHook:

})

我的pre hook函數(shù)的go代碼。PutLifecycleEventHookExecutionStatus如果預(yù)掛鉤成功,則告訴代碼部署。不幸的是,如果部署消息失敗,您在 cdk 部署輸出中獲得的消息將毫無用處,因此您需要檢查掛鉤前/后日志。


為了實(shí)際運(yùn)行集成測(cè)試,我只需調(diào)用 lambda 并檢查是否發(fā)生錯(cuò)誤。


package main


import (

    "encoding/base64"

    "fmt"

    "log"

    "os"


    "github.com/aws/aws-lambda-go/lambda"

    "github.com/aws/aws-sdk-go/aws"

    "github.com/aws/aws-sdk-go/aws/session"

    "github.com/aws/aws-sdk-go/service/codedeploy"

    lambdaService "github.com/aws/aws-sdk-go/service/lambda"

)


var svc *codedeploy.CodeDeploy

var lambdaSvc *lambdaService.Lambda


type codeDeployEvent struct {

    DeploymentId                  string `json:"deploymentId"`

    LifecycleEventHookExecutionId string `json:"lifecycleEventHookExecutionId"`

}


func handler(e codeDeployEvent) error {

    params := &codedeploy.PutLifecycleEventHookExecutionStatusInput{

        DeploymentId:                  &e.DeploymentId,

        LifecycleEventHookExecutionId: &e.LifecycleEventHookExecutionId,

    }

    err := handle()

    if err != nil {

        log.Println(err)

        params.Status = aws.String(codedeploy.LifecycleEventStatusFailed)

    } else {

        params.Status = aws.String(codedeploy.LifecycleEventStatusSucceeded)

    }


    _, err = svc.PutLifecycleEventHookExecutionStatus(params)

    if err != nil {

        return fmt.Errorf("failed putting the lifecycle event hook execution status. the status was %s", *params.Status)

    }


    return nil

}


func handle() error {

    functionName := os.Getenv("FUNCTION_NAME")

    if functionName == "" {

        return fmt.Errorf("FUNCTION_NAME not set")

    }

    log.Printf("function name: %s", functionName)


    // invoke lambda via sdk

    input := &lambdaService.InvokeInput{

        FunctionName:   &functionName,

        Payload:        nil,

        LogType:        aws.String(lambdaService.LogTypeTail),                   // returns the log in the response

        InvocationType: aws.String(lambdaService.InvocationTypeRequestResponse), // synchronous - default

    }

    err := input.Validate()

    if err != nil {

        return fmt.Errorf("validating the input failed: %v", err)

    }


    resp, err := lambdaSvc.Invoke(input)

    if err != nil {

        return fmt.Errorf("failed to invoke lambda: %v", err)

    }


    decodeString, err := base64.StdEncoding.DecodeString(*resp.LogResult)

    if err != nil {

        return fmt.Errorf("failed to decode the log: %v", err)

    }

    log.Printf("log result: %s", decodeString)


    if resp.FunctionError != nil {

        return fmt.Errorf("lambda was invoked but returned error: %s", *resp.FunctionError)

    }

    return nil

}


func main() {

    sess, err := session.NewSession()

    if err != nil {

        return

    }

    svc = codedeploy.New(sess)

    lambdaSvc = lambdaService.New(sess)

    lambda.Start(handler)

}


查看完整回答
反對(duì) 回復(fù) 2023-05-22
  • 2 回答
  • 0 關(guān)注
  • 169 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)