Skip to content

Commit

Permalink
Added lambda support and goreleaser.
Browse files Browse the repository at this point in the history
  • Loading branch information
sdewitt-newrelic committed May 3, 2023
1 parent 20432ef commit 60d31b1
Show file tree
Hide file tree
Showing 10 changed files with 348 additions and 2 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -120,3 +120,7 @@ dist
.pnp.*

config.yml

dist/
cf-params.json
.build.env
72 changes: 72 additions & 0 deletions .goreleaser.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
# This is an example .goreleaser.yml file with some sensible defaults.
# Make sure to check the documentation at https://goreleaser.com
before:
hooks:
# You may remove this if you don't use go modules.
- go mod tidy

builds:
- id: standalone
main: ./cmd/nr-entity-tag-sync/nr-entity-tag-sync.go
env:
- CGO_ENABLED=0
goos:
- linux
#- windows
#- darwin
goarch:
- amd64
- id: lambda
main: ./cmd/nr-entity-tag-sync-lambda/nr-entity-tag-sync-lambda.go
binary: nr-entity-tag-sync-lambda
env:
- CGO_ENABLED=0
goos:
- linux
goarch:
- amd64

dockers:
- id: lambda-image
ids:
- lambda
image_templates:
- "{{ .Env.AWS_ECR_REPO_URI }}:latest"
- "{{ .Env.AWS_ECR_REPO_URI }}:{{ .Tag }}"
dockerfile: ./deployments/Dockerfile-lambda
extra_files:
- configs/config.yml

archives:
- format: tar.gz
builds:
- standalone
# this name template makes the OS and Arch compatible with the results of uname.
name_template: >-
{{ .ProjectName }}_
{{- title .Os }}_
{{- if eq .Arch "amd64" }}x86_64
{{- else if eq .Arch "386" }}i386
{{- else }}{{ .Arch }}{{ end }}
{{- if .Arm }}v{{ .Arm }}{{ end }}
# use zip for windows archives
format_overrides:
- goos: windows
format: zip

checksum:
name_template: 'checksums.txt'

snapshot:
name_template: "{{ incpatch .Version }}-next"
changelog:
sort: asc
filters:
exclude:
- '^docs:'
- '^test:'

# The lines beneath this are called `modelines`. See `:help modeline`
# Feel free to remove those if you don't want/use them.
# yaml-language-server: $schema=https://goreleaser.com/static/schema.json
# vim: set ts=2 sw=2 tw=0 fo=cnqoj
39 changes: 39 additions & 0 deletions cmd/nr-entity-tag-sync-lambda/nr-entity-tag-sync-lambda.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package main

import (
"context"
"fmt"

_ "github.com/newrelic/nr-entity-tag-sync/internal/provider/servicenow"

"github.com/aws/aws-lambda-go/lambda"
"github.com/newrelic/nr-entity-tag-sync/internal/sync"
"github.com/newrelic/nr-entity-tag-sync/pkg/interop"
)

type TagSyncResult struct {
Success bool
Message error
}

func HandleRequest(ctx context.Context) (TagSyncResult, error) {
i, err := interop.NewInteroperability()
if err != nil {
retErr := fmt.Errorf("failed to create interop: %s", err)
return TagSyncResult{false, retErr}, retErr
}

defer i.Shutdown()

err = sync.Sync(i)
if err != nil {
retErr := fmt.Errorf("sync failed: %s", err)
return TagSyncResult{false, retErr}, retErr
}

return TagSyncResult{true, nil}, nil
}

func main() {
lambda.Start(HandleRequest)
}
4 changes: 4 additions & 0 deletions deployments/Dockerfile-lambda
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
FROM public.ecr.aws/lambda/provided:al2
COPY nr-entity-tag-sync-lambda /main
COPY configs/config.yml /var/task/configs/config.yml
ENTRYPOINT [ "/main" ]
46 changes: 46 additions & 0 deletions deployments/cf-params.sample.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
[
{
"ParameterKey": "ImageRepoUri",
"ParameterValue": "12345.dkr.ecr.aws_region.amazonaws.com/example-repo-name"
},
{
"ParameterKey": "ImageTag",
"ParameterValue": "1.0.0"
},
{
"ParameterKey": "ExecRoleArn",
"ParameterValue": "arn:aws:iam::12345:role/my-execute-lambda-role"
},
{
"ParameterKey": "FunctionName",
"ParameterValue": "NrEntityTagSync"
},
{
"ParameterKey": "FunctionTimeout",
"ParameterValue": "120"
},
{
"ParameterKey": "FunctionMemorySize",
"ParameterValue": "512"
},
{
"ParameterKey": "NRLicenseKey",
"ParameterValue": "123456"
},
{
"ParameterKey": "NRLogLevel",
"ParameterValue": "INFO"
},
{
"ParameterKey": "UserApiKey",
"ParameterValue": "123456"
},
{
"ParameterKey": "SnowApiUser",
"ParameterValue": "admin"
},
{
"ParameterKey": "SnowApiPassword",
"ParameterValue": "admin"
}
]
127 changes: 127 additions & 0 deletions deployments/cf-template.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
AWSTemplateFormatVersion: '2010-09-09'
Description: Sample CloudFormation template for deploying the AWS Lambda image.

Parameters:
#
# URI of the ECR repository.
#
ImageRepoUri:
Type: String
Description: URI of the ECR repository.
#
# ECR image tag.
#
ImageTag:
Type: String
Description: ECR image tag.
Default: latest
#
# ARN of the lambda execution role used to execute the lambda.
#
# Used during deployment.
#
ExecRoleArn:
Type: String
Description: ARN of the IAM role used for lambda execution.
AllowedPattern: 'arn:(aws[a-zA-Z-]*)?:iam::\d{12}:role/?[a-zA-Z_0-9+=,.@\-_/]+'
#
# Name of the lambda function.
#
# Used during deployment.
#
FunctionName:
Type: String
Description: Name of the lambda function.
Default: NrEntityTagSync
#
# Timeout (in seconds) for the lambda function.
#
# Used during deployment.
#
FunctionTimeout:
Type: Number
Description: Timeout (in seconds) for the lambda function.
MinValue: 1
MaxValue: 900
Default: 60
#
# Memory size for the lambda function.
#
# Used during deployment.
#
FunctionMemorySize:
Type: Number
Description: Memory size for the lambda function.
MinValue: 128
MaxValue: 10240
Default: 256
#
# New Relic license key for the New Relic Go APM agent.
#
NRLicenseKey:
Type: String
Description: Your New Relic license key.
AllowedPattern: '[a-zA-Z0-9\-_]*'
Default: ''
#
# Log level for the New Relic AWS lambda extension. Defaults to 'INFO'.
#
# Used at runtime by the New Relic AWS lambda extension.
# See https://docs.newrelic.com/docs/serverless-function-monitoring/aws-lambda-monitoring/enable-lambda-monitoring/account-linking/#env-var
#
NRLogLevel:
Type: String
Description: Log level for the New Relic Lamba extension.
Default: INFO
AllowedValues:
- INFO
- DEBUG
#
# New Relic User API key used for GraphQL Nerdstorage queries and mutations.
#
UserApiKey:
Type: String
Description: The New Relic User API key to use.
AllowedPattern: '[a-zA-Z0-9._\-]*'
Default: ''
#
# Service Now API user name used by the Service Now provider.
#
SnowApiUser:
Type: String
Description: The Service Now API user.
Default: ''
#
# Service Now API password name used by the Service Now provider.
#
SnowApiPassword:
Type: String
Description: The Service Now API password.
Default: ''

Resources:

#
# The lambda.
#

NrEntityTagSync:
Type: AWS::Lambda::Function
Properties:
PackageType: Image
Code:
ImageUri: !Sub ${ImageRepoUri}:${ImageTag}
Role: !Sub ${ExecRoleArn}
Description: A lambda for syncing tags between external entities and New Relic entities.
FunctionName: !Sub ${FunctionName}
Timeout: !Ref FunctionTimeout
MemorySize: !Ref FunctionMemorySize
Environment:
Variables:
# Lambda environment variables
NEW_RELIC_LICENSE_KEY: !Sub ${NRLicenseKey}
NEW_RELIC_API_KEY: !Sub ${UserApiKey}

# Service Now provider variables
NR_CMDB_SNOW_APIUSER: !Sub ${SnowApiUser}
NR_CMDB_SNOW_APIPASSWORD: !Sub ${SnowApiPassword}
5 changes: 3 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@ module github.com/newrelic/nr-entity-tag-sync
go 1.20

require (
github.com/aws/aws-lambda-go v1.40.0
github.com/hasura/go-graphql-client v0.9.2
github.com/newrelic/go-agent/v3 v3.21.0
github.com/newrelic/go-agent/v3/integrations/logcontext-v2/nrlogrus v1.0.0
github.com/sirupsen/logrus v1.9.0
github.com/spf13/cast v1.5.0
github.com/spf13/viper v1.15.0
)

Expand All @@ -14,13 +17,11 @@ require (
github.com/golang/protobuf v1.5.2 // indirect
github.com/google/uuid v1.3.0 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/hasura/go-graphql-client v0.9.2 // indirect
github.com/klauspost/compress v1.16.5 // indirect
github.com/magiconair/properties v1.8.7 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/pelletier/go-toml/v2 v2.0.6 // indirect
github.com/spf13/afero v1.9.3 // indirect
github.com/spf13/cast v1.5.0 // indirect
github.com/spf13/jwalterweatherman v1.1.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/subosito/gotenv v1.4.2 // indirect
Expand Down

0 comments on commit 60d31b1

Please sign in to comment.