Skip to content

Commit

Permalink
Merge branch 'main' into provided-panic
Browse files Browse the repository at this point in the history
  • Loading branch information
bmoffatt authored Jan 24, 2024
2 parents b8a6cfc + 3a93ed1 commit 7ba157c
Show file tree
Hide file tree
Showing 21 changed files with 583 additions and 47 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,6 @@ jobs:

- name: golangci-golint
run: |
curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s v1.52.1
curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s v1.55.2
./bin/golangci-lint run -v ./...
52 changes: 50 additions & 2 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,53 @@ run:
linters-settings:
stylecheck:
go: "1.20"
checks: ["all"]
initialisms: ["AWS", "ARN", "IAM", "MQTT", "ACL", "API", "ASCII", "CPU", "CSS", "DNS", "EOF", "GUID", "HTML", "HTTP", "HTTPS", "ID", "IP", "JSON", "QPS", "RAM", "RPC", "SLA", "SMTP", "SQL", "SSH", "TCP", "TLS", "TTL", "UDP", "UI", "GID", "UID", "UUID", "URI", "URL", "UTF8", "VM", "XML", "XMPP", "XSRF", "XSS", "SIP", "RTP", "AMQP", "DB", "TS", "VPC"]
checks:
- "all"
initialisms:
- "ACL"
- "AMQP"
- "API"
- "ARN"
- "ASCII"
- "AWS"
- "CPU"
- "CSS"
- "DB"
- "DNS"
- "EOF"
- "GID"
- "GUID"
- "HTML"
- "HTTP"
- "HTTPS"
- "IAM"
- "ID"
- "IP"
- "JSON"
- "MQTT"
- "QPS"
- "RAM"
- "RPC"
- "RTP"
- "SIP"
- "SLA"
- "SMTP"
- "SQL"
- "SSH"
- "TCP"
- "TLS"
- "TS"
- "TTL"
- "UDP"
- "UI"
- "UID"
- "URI"
- "URL"
- "UTF8"
- "UUID"
- "VM"
- "VPC"
- "XML"
- "XMPP"
- "XSRF"
- "XSS"
32 changes: 28 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,16 +43,26 @@ func main() {

# Building your function

Preparing a binary to deploy to AWS Lambda requires that it is compiled for Linux and placed into a .zip file.
Preparing a binary to deploy to AWS Lambda requires that it is compiled for Linux and placed into a .zip file. When using the `provided`, `provided.al2`, or `provided.al2023` runtime, the executable within the .zip file should be named `bootstrap`. Lambda's default architecture is `x86_64`, so when cross compiling from a non-x86 environment, the executable should be built with `GOARCH=amd64`. Likewise, if the Lambda function will be [configured to use ARM](https://docs.aws.amazon.com/lambda/latest/dg/foundation-arch.html), the executable should built with `GOARCH=arm64`.

## For developers on Linux and macOS
``` shell
# Remember to build your handler executable for Linux!
# When using the `provided.al2` runtime, the handler executable should be named `bootstrap`
GOOS=linux GOARCH=amd64 go build -o bootstrap main.go
zip lambda-handler.zip bootstrap
```

## For developers on Linux

On Linux, the Go compiler's default behavior is to link the output executable to the system libc for some standard library functionality (for example, DNS lookups). If the build environment is using a Linux distribution with a GNU libc version newer than the deployment environment, the application when deployed to Lambda may fail with an error like ``/lib64/libc.so.6: version `GLIBC_X.YZ' not found``.

Most Go applications do not require linking to the system libc. This behavior can be disabled by using the `CGO_ENABLED` environment variable.

```
CGO_ENABLED=0 go build -o bootstrap main.go
zip lambda-handler.zip bootstrap
```

See [Using CGO](#using-cgo)

## For developers on Windows

Windows developers may have trouble producing a zip file that marks the binary as executable on Linux. To create a .zip that will work on AWS Lambda, the `build-lambda-zip` tool may be helpful.
Expand Down Expand Up @@ -81,6 +91,20 @@ $env:CGO_ENABLED = "0"
go build -o bootstrap main.go
~\Go\Bin\build-lambda-zip.exe -o lambda-handler.zip bootstrap
```

## Using CGO

For applications that require CGO, the build environment must be using a GNU libc version installed compatible with the target Lambda runtime. Otherwise, execution may fail with errors like ``/lib64/libc.so.6: version `GLIBC_X.YZ' not found``.

| Lambda runtime | GLIBC version
| ----- | ---
| `provided.al2023` | 2.34
| `provided.al2` | 2.26
| `provided` and `go1.x` | 2.17


Alternatively, Lambda supports container images as a deployment package alternative to .zip files. For more information, refer to the official documentation for [working with with container images](https://docs.aws.amazon.com/lambda/latest/dg/images-create.html).

# Deploying your functions

To deploy your function, refer to the official documentation for [deploying using the AWS CLI, AWS Cloudformation, and AWS SAM](https://docs.aws.amazon.com/lambda/latest/dg/deploying-lambda-apps.html).
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

# Sample Function

The following is a Lambda function that receives Amazon CloudWatch event record data as input and writes event detail to Lambda's CloudWatch Logs. Note that by default anything written to Console will be logged as CloudWatch Logs events.
The following is a Lambda function that receives Amazon EventBridge event record data as input and writes event detail to Lambda's CloudWatch Logs. Note that by default anything written to Console will be logged as CloudWatch Logs events.

```go
import (
Expand All @@ -11,7 +11,7 @@ import (
"github.com/aws/aws-lambda-go/events"
)

func handler(ctx context.Context, event events.CloudWatchEvent) {
func handler(ctx context.Context, event events.EventBridgeEvent) {
fmt.Printf("Detail = %s\n", event.Detail)
}
```
Expand Down
83 changes: 83 additions & 0 deletions events/README_S3_Object_Lambda.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
# Sample Function

The following is a sample class and Lambda function that receives Amazon S3 Object Lambda event record data as an input and returns an object metadata output.

```go

// main.go
package main

import (
"context"
"crypto/md5"
"encoding/hex"
"encoding/json"
"io/ioutil"
"net/http"
"github.com/aws/aws-lambda-go/lambda"
"github.com/aws/aws-lambda-go/events"
"github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/service/s3"
)

func handler(ctx context.Context, event events.S3ObjectLambdaEvent) error {
url := event.GetObjectContext.InputS3Url
resp, err := http.Get(url)
if err != nil {
return err
}
defer resp.Body.Close()
bodyBytes, err := ioutil.ReadAll(resp.Body)
if err != nil {
return err
}
transformedObject := TransformedObject{
Metadata: Metadata{
Length: len(bodyBytes),
Md5: toMd5(bodyBytes),
},
}
jsonData, err := json.Marshal(transformedObject)
if err != nil {
return err
}
cfg, err := config.LoadDefaultConfig(context.TODO())
if err != nil {
return err
}
svc := s3.NewFromConfig(cfg)
input := &s3.WriteGetObjectResponseInput{
RequestRoute: &event.GetObjectContext.OutputRoute,
RequestToken: &event.GetObjectContext.OutputToken,
Body: strings.NewReader(string(jsonData)),
}
res, err := svc.WriteGetObjectResponse(ctx, input)
if err != nil {
return err
}
fmt.Printf("%v", res)
return nil
}

func toMd5(data []byte) string {
hasher := md5.New()
hasher.Write(data)
hash := hasher.Sum(nil)

return hex.EncodeToString(hash)
}

type TransformedObject struct {
Metadata Metadata `json:"metadata"`
}

type Metadata struct {
Length int `json:"length"`
Md5 string `json:"md5"`
}

func main() {
lambda.Start(handler)
}

```
17 changes: 1 addition & 16 deletions events/attributevalue_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -215,25 +215,10 @@ func TestAccessWithWrongTypePanics(t *testing.T) {
var av DynamoDBAttributeValue
err := json.Unmarshal([]byte(testCase.input), &av)
assert.Nil(t, err)
// may use PanicsWithValue(expectedError) when it is available
assertPanicsWithValue(t, testCase.expectedError, func() { testCase.accessor(av) })
assert.PanicsWithValue(t, testCase.expectedError, func() { testCase.accessor(av) })
}
}

func assertPanicsWithValue(t *testing.T, expected error, action func()) {
defer func() {
r := recover()
if r == nil {
t.Errorf("Should have panicked")
}
if r != expected {
t.Errorf("should have panicked with value %v but panicked with value %v", expected, r)
}
}()

action()
}

func TestMarshalAndUnmarshalString(t *testing.T) {
const inputString = "INPUT STRING"
inputValue := NewStringAttribute(inputString)
Expand Down
5 changes: 3 additions & 2 deletions events/cloudwatch_events.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ import (
"time"
)

// CloudWatchEvent is the outer structure of an event sent via CloudWatch Events.
// For examples of events that come via CloudWatch Events, see https://docs.aws.amazon.com/AmazonCloudWatch/latest/events/EventTypes.html
// CloudWatchEvent is the outer structure of an event sent via EventBridge serverless service.
type CloudWatchEvent struct {
Version string `json:"version"`
ID string `json:"id"`
Expand All @@ -18,3 +17,5 @@ type CloudWatchEvent struct {
Resources []string `json:"resources"`
Detail json.RawMessage `json:"detail"`
}

type EventBridgeEvent = CloudWatchEvent
2 changes: 0 additions & 2 deletions events/codedeploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ const (
CodeDeployDeploymentStateSuccess CodeDeployDeploymentState = "SUCCESS"
)

// CodeDeployEvent is documented at:
// https://docs.aws.amazon.com/AmazonCloudWatch/latest/events/EventTypes.html#acd_event_types
type CodeDeployEvent struct {
// AccountID is the id of the AWS account from which the event originated.
AccountID string `json:"account"`
Expand Down
4 changes: 2 additions & 2 deletions events/codepipeline_cloudwatch.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,6 @@ const (
CodePipelineActionStateCanceled CodePipelineActionState = "CANCELED"
)

// CodePipelineEvent is documented at:
// https://docs.aws.amazon.com/AmazonCloudWatch/latest/events/EventTypes.html#codepipeline_event_type
type CodePipelineCloudWatchEvent struct {
// Version is the version of the event's schema.
Version string `json:"version"`
Expand Down Expand Up @@ -76,6 +74,8 @@ type CodePipelineCloudWatchEvent struct {
Detail CodePipelineEventDetail `json:"detail"`
}

type CodePipelineEventBridgeEvent = CodePipelineCloudWatchEvent

type CodePipelineEventDetail struct {
Pipeline string `json:"pipeline"`

Expand Down
3 changes: 2 additions & 1 deletion events/codepipeline_cloudwatch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ package events

import (
"encoding/json"
"github.com/stretchr/testify/require"
"io/ioutil" //nolint: staticcheck
"testing"
"time"

"github.com/stretchr/testify/require"
)

func TestUnmarshalCodePipelineEvent(t *testing.T) {
Expand Down
Loading

0 comments on commit 7ba157c

Please sign in to comment.