Skip to content

Commit 0365316

Browse files
committed
events: add Secrets Manager rotation event (aws#291)
resolves aws#291
1 parent c4e28da commit 0365316

4 files changed

+84
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# Sample Function
2+
3+
The following is a sample Lambda function that handles a SecretsManager secret rotation event.
4+
5+
```go
6+
package main
7+
8+
import (
9+
"fmt"
10+
"context"
11+
12+
"github.com/aws/aws-lambda-go/lambda"
13+
"github.com/aws/aws-lambda-go/events"
14+
)
15+
16+
func handler(ctx context.Context, event SecretsManagerSecretRotationEvent) error {
17+
fmt.Printf("rotating secret %s with token %s\n",
18+
event.SecretId, event.ClientRequestToken)
19+
20+
switch event.Step {
21+
case "createSecret":
22+
// create
23+
case "setSecret":
24+
// set
25+
case "finishSecret":
26+
// finish
27+
case "testSecret":
28+
// test
29+
}
30+
31+
return nil
32+
}
33+
34+
35+
func main() {
36+
lambda.Start(handler)
37+
}
38+
```

events/secretsmanager.go

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package events
2+
3+
// SecretsManagerSecretRotationEvent is the event passed to a Lambda function to handle
4+
// automatic secret rotation.
5+
//
6+
// https://docs.aws.amazon.com/secretsmanager/latest/userguide/rotating-secrets.html#rotate-secrets_how
7+
type SecretsManagerSecretRotationEvent struct {
8+
Step string `json:"Step"`
9+
SecretId string `json:"SecretId"`
10+
ClientRequestToken string `json:"ClientRequestToken"`
11+
}

events/secretsmanager_test.go

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package events
2+
3+
import (
4+
"encoding/json"
5+
"testing"
6+
7+
"github.com/aws/aws-lambda-go/events/test"
8+
"github.com/stretchr/testify/assert"
9+
)
10+
11+
func TestSecretsManagerSecretRotationEventMarshaling(t *testing.T) {
12+
13+
// 1. read JSON from file
14+
inputJSON := test.ReadJSONFromFile(t, "./testdata/secretsmanager-secret-rotation-event.json")
15+
16+
// 2. de-serialize into Go object
17+
var inputEvent SecretsManagerSecretRotationEvent
18+
if err := json.Unmarshal(inputJSON, &inputEvent); err != nil {
19+
t.Errorf("could not unmarshal event. details: %v", err)
20+
}
21+
22+
// 3. serialize to JSON
23+
outputJSON, err := json.Marshal(inputEvent)
24+
if err != nil {
25+
t.Errorf("could not marshal event. details: %v", err)
26+
}
27+
28+
// 4. check result
29+
assert.JSONEq(t, string(inputJSON), string(outputJSON))
30+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"Step": "createSecret",
3+
"SecretId": "arn:aws:secretsmanager:us-east-1:111122223333:secret:id-ABCD1E",
4+
"ClientRequestToken": "1ab23456-cde7-8912-34fg-h56i78j9k12l"
5+
}

0 commit comments

Comments
 (0)