Skip to content

Commit

Permalink
feat: first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
jta committed Sep 26, 2023
0 parents commit 5a011ee
Show file tree
Hide file tree
Showing 162 changed files with 26,496 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Ignore build folder
.aws-sam/
90 changes: 90 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
# SAM test

This repo attempts to validate:

- [x] Go code producing multiple binaries with unit tests
- [x] Multiple AWS SAM applications
- [x] An application that references other published applications


## Testing

The codebase looks like a standard golang project, so all the standard Go tooling should just work, e.g:

```
→ go test ./...
ok github.com/observeinc/aws-sam-testing/cmd/ec2 (cached)
? github.com/observeinc/aws-sam-testing/model/aws/ec2 [no test files]
? github.com/observeinc/aws-sam-testing/model/aws/ec2/marshaller [no test files]
ok github.com/observeinc/aws-sam-testing/cmd/hello-world (cached)
```

## Building and deploying

Each AWS SAM template lives under `apps/`. You can use the `sam` cli to build, invoke and deploy the cloudformation stack for testing.

```
→ cd apps/hello-world
→ sam build
Starting Build use cache
Valid cache found, copying previously built resources for following functions (HelloWorldFunction)
Build Succeeded
Built Artifacts : .aws-sam/build
Built Template : .aws-sam/build/template.yaml
```

## Publishing apps

Each SAM app can be packaged and published to the AWS Serverless Application Repository.

1. Bump the `SemanticVersion` in `template.yaml`:

```
Metadata:
AWS::ServerlessRepo::Application:
Name: hello-world-thing
Description: A hello world
Author: Observe Inc
SpdxLicenseId: Apache-2.0
ReadmeUrl: README.md
HomePageUrl: https://github.com/observeinc/aws-sam-testing
SemanticVersion: 0.0.2
SourceCodeUrl: https://github.com/observeinc/aws-sam-testing
```

2. Package the template:

```
→ sam package --template-file template.yaml --output-template-file output.yaml
Managed S3 bucket: aws-sam-cli-managed-default-samclisourcebucket-1d9p458evhgwf
A different default S3 bucket can be set in samconfig.toml
Or by specifying --s3-bucket explicitly.
File with same data already exists at 410049026555143131a24cfafb8eb862, skipping upload
Uploading to bae5b6e5768f0a6e85e1b9c52dddcead 1021 / 1021 (100.00%)
Successfully packaged artifacts and wrote output template to file output.yaml.
Execute the following command to deploy the packaged template
sam deploy --template-file /Users/joao/Code/aws-sam-testing/apps/hello-world/output.yaml --stack-name <YOUR STACK NAME>
```

3. Publish the packaged template (`sam publish --template output.yaml`)

```
→ sam publish --template output.yaml
Publish Succeeded
The following metadata of application "arn:aws:serverlessrepo:us-west-2:739672403694:applications/hello-world-thing" has been updated:
{
"Description": "A hello world",
"Author": "Observe Inc",
"ReadmeUrl": "s3://aws-sam-cli-managed-default-samclisourcebucket-1d9p458evhgwf/410049026555143131a24cfafb8eb862",
"HomePageUrl": "https://github.com/observeinc/aws-sam-testing",
"SemanticVersion": "0.0.2",
"SourceCodeUrl": "https://github.com/observeinc/aws-sam-testing"
}
Click the link below to view your application in AWS console:
https://console.aws.amazon.com/serverlessrepo/home?region=us-west-2#/published-applications/arn:aws:serverlessrepo:us-west-2:739672403694:applications~hello-world-thing
```
4 changes: 4 additions & 0 deletions apps/ec2/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
EC2
===

This is a readme
15 changes: 15 additions & 0 deletions apps/ec2/events/event.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"id":"7bf73129-1428-4cd3-a780-95db273d1602",
"detail-type":"EC2 Instance State-change Notification",
"source":"aws.ec2",
"account":"123456789012",
"time":"2015-11-11T21:29:54Z",
"region":"us-east-1",
"resources":[
"arn:aws:ec2:us-east-1:123456789012:instance/i-abcd1111"
],
"detail":{
"instance-id":"i-abcd1111",
"state":"pending"
}
}
56 changes: 56 additions & 0 deletions apps/ec2/packaged.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: 'EC2 EventBridge thing
'
Metadata:
AWS::ServerlessRepo::Application:
Name: ec2-thing
Description: An ec2 thing
Author: Observe Inc
SpdxLicenseId: Apache-2.0
ReadmeUrl: s3://aws-sam-cli-managed-default-samclisourcebucket-1d9p458evhgwf/4807cb4792f21dcb568ec12ab3faa7ed
HomePageUrl: https://github.com/observeinc/aws-sam-testing
SemanticVersion: '0.1.0'
SourceCodeUrl: https://github.com/observeinc/aws-sam-testing
Globals:
Function:
Timeout: 20
MemorySize: 128
Resources:
Function:
Type: AWS::Serverless::Function
Metadata:
BuildMethod: go1.x
SamResourceId: Function
Properties:
CodeUri: s3://aws-sam-cli-managed-default-samclisourcebucket-1d9p458evhgwf/22400a3296b0cc05e20720ec29d15daf
Handler: bootstrap
Runtime: provided.al2
Architectures:
- x86_64
Environment:
Variables:
PARAM1: VALUE
Events:
HelloWorld:
Type: EventBridgeRule
Properties:
Pattern:
source:
- aws.ec2
detail-type:
- EC2 Instance State-change Notification
Outputs:
Function:
Description: Lambda Function ARN
Value:
Fn::GetAtt:
- Function
- Arn
FunctionIamRole:
Description: Implicit IAM Role created for function
Value:
Fn::GetAtt:
- FunctionRole
- Arn
31 changes: 31 additions & 0 deletions apps/ec2/samconfig.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# More information about the configuration file can be found here:
# https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/serverless-sam-cli-config.html
version = 0.1

[default]
[default.global.parameters]
stack_name = "ec2"

[default.build.parameters]
cached = true
parallel = true

[default.validate.parameters]
lint = true

[default.deploy.parameters]
capabilities = "CAPABILITY_IAM"
confirm_changeset = true
resolve_s3 = true

[default.package.parameters]
resolve_s3 = true

[default.sync.parameters]
watch = true

[default.local_start_api.parameters]
warm_containers = "EAGER"

[default.local_start_lambda.parameters]
warm_containers = "EAGER"
57 changes: 57 additions & 0 deletions apps/ec2/template.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: >
EC2 EventBridge thing
Metadata:
AWS::ServerlessRepo::Application:
Name: ec2-thing
Description: An ec2 thing
Author: Observe Inc
SpdxLicenseId: Apache-2.0
ReadmeUrl: README.md
HomePageUrl: https://github.com/observeinc/aws-sam-testing
SemanticVersion: 0.1.0
SourceCodeUrl: https://github.com/observeinc/aws-sam-testing


# More info about Globals: https://github.com/awslabs/serverless-application-model/blob/master/docs/globals.rst
Globals:
Function:
Timeout: 20
MemorySize: 128

Resources:
Function:
Type: AWS::Serverless::Function # More info about Function Resource: https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#awsserverlessfunction
Metadata:
BuildMethod: go1.x
Properties:
CodeUri: ../../cmd/ec2/
Handler: bootstrap
Runtime: provided.al2
Architectures:
- x86_64
Environment: # More info about Env Vars: https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#environment-object
Variables:
PARAM1: VALUE
Events:
HelloWorld:
Type: EventBridgeRule # More info about EventBridge Event Source: https://github.com/aws/serverless-application-model/blob/master/versions/2016-10-31.md#eventbridgerule
Properties:
Pattern:
source:
- aws.ec2
detail-type:
- EC2 Instance State-change Notification

Outputs:
# ServerlessRestApi is an implicit API created out of Events key under Serverless::Function
# Find out more about other implicit resources you can reference within SAM
# https://github.com/awslabs/serverless-application-model/blob/master/docs/internals/generated_resources.rst#api
Function:
Description: "Lambda Function ARN"
Value: !GetAtt Function.Arn
FunctionIamRole:
Description: "Implicit IAM Role created for function"
Value: !GetAtt FunctionRole.Arn
1 change: 1 addition & 0 deletions apps/group/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# An group
31 changes: 31 additions & 0 deletions apps/group/samconfig.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# More information about the configuration file can be found here:
# https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/serverless-sam-cli-config.html
version = 0.1

[default]
[default.global.parameters]
stack_name = "group"

[default.build.parameters]
cached = true
parallel = true

[default.validate.parameters]
lint = true

[default.deploy.parameters]
capabilities = "CAPABILITY_IAM"
confirm_changeset = true
resolve_s3 = true

[default.package.parameters]
resolve_s3 = true

[default.sync.parameters]
watch = true

[default.local_start_api.parameters]
warm_containers = "EAGER"

[default.local_start_lambda.parameters]
warm_containers = "EAGER"
100 changes: 100 additions & 0 deletions apps/group/template.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: >
Hello world.
Metadata:
AWS::ServerlessRepo::Application:
Name: group
Description: A group of applications
Author: Observe Inc
SpdxLicenseId: Apache-2.0
ReadmeUrl: README.md
HomePageUrl: https://github.com/observeinc/aws-sam-testing
SemanticVersion: 0.0.3
SourceCodeUrl: https://github.com/observeinc/aws-sam-testing


# More info about Globals: https://github.com/awslabs/serverless-application-model/blob/master/docs/globals.rst
Globals:
Function:
Timeout: 5
MemorySize: 128

Parameters:
EnableEC2:
Type: String
Default: false
Description: >-
Enable EC2 stack
AllowedValues:
- true
- false
Verbosity:
Type: Number
Default: 3
Description: >-
Logging verbosity.
TrailEnableLogFileValidation:
Type: String
Default: false
Description: Indicates whether CloudTrail validates the integrity of log files.
AllowedValues:
- true
- false
TrailIncludeGlobalEvents:
Type: String
Default: true
Description: >-
Indicates whether the trail is publishing events from global services,
such as IAM, to the log files.
AllowedValues:
- true
- false
TrailEnabled:
Type: String
Default: true
Description: >-
Whether to collect CloudTrail data or not. Only disable if you already submit Cloudtrail data separately.
AllowedValues:
- true
- false
TrailMultiRegion:
Type: String
Default: false
Description: >-
Indicates whether the CloudTrail trail is created in the region in which
you create the stack (false) or in all regions (true).
AllowedValues:
- true
- false
TrailExcludeManagementEventSources:
Type: CommaDelimitedList
Default: "kms.amazonaws.com,rdsdata.amazonaws.com"
Description: >-
A comma-separated list of Management Event Sources to exclude.
See the following link for more info:
https://docs.aws.amazon.com/awscloudtrail/latest/userguide/logging-management-events-with-cloudtrail.html
Conditions:
EC2IsEnabled: !Equals
- Ref: EnableEC2
- true

Resources:
HelloWorld:
Type: AWS::Serverless::Application
Properties:
Location:
ApplicationId: arn:aws:serverlessrepo:us-west-2:739672403694:applications/hello-world-thing
SemanticVersion: 0.0.1
EC2:
Type: AWS::Serverless::Application
Condition: EC2IsEnabled
Properties:
Location:
ApplicationId: arn:aws:serverlessrepo:us-west-2:739672403694:applications/ec2-thing
SemanticVersion: 0.1.0

Loading

0 comments on commit 5a011ee

Please sign in to comment.