Skip to content

Commit e852640

Browse files
author
Gernot Glawe
committed
goformation
1 parent f28cde2 commit e852640

26 files changed

+7006
-1
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,4 @@ infrastructure-as-go/cdk-go/thecdkbook/chapter6/customresource/app/dist
2020
infrastructure-as-go/cdk-go/thecdkbook/chapter6/customresource/dist
2121
architectures/serverless/app/dist
2222
architectures/serverless/infra/dist
23+
infrastructure-as-go/goformation/dynamodb/dist

buildspec.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ batch:
1313
- identifier: architecture_container
1414
env:
1515
variables:
16-
BUILD_ID: architecture_serverless
16+
BUILD_ID: architecture_container
1717
BUILD_BASE_APP: architectures/container/app
1818
BUILD_BASE_INFRA: architectures/container/infra/2-table
1919
ignore-failure: true
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Binaries for programs and plugins
2+
*.exe
3+
*.exe~
4+
*.dll
5+
*.so
6+
*.dylib
7+
8+
# Test binary, built with `go test -c`
9+
*.test
10+
11+
# Output of the go coverage tool, specifically when used with LiteIDE
12+
*.out
13+
14+
# go.sum should be committed
15+
!go.sum
16+
17+
# CDK asset staging directory
18+
.cdk.staging
19+
cdk.out
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# Welcome to your CDK Go project!
2+
3+
This is a blank project for Go development with CDK.
4+
5+
**NOTICE**: Go support is still in Developer Preview. This implies that APIs may
6+
change while we address early feedback from the community. We would love to hear
7+
about your experience through GitHub issues.
8+
9+
## Useful commands
10+
11+
* `cdk deploy` deploy this stack to your default AWS account/region
12+
* `cdk diff` compare deployed stack with current state
13+
* `cdk synth` emits the synthesized CloudFormation template
14+
* `go test` run unit tests
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{
2+
"app": "go mod download && go run dynamodb-cdk-go.go",
3+
"watch": {
4+
"include": [
5+
"**"
6+
],
7+
"exclude": [
8+
"README.md",
9+
"cdk*.json",
10+
"go.mod",
11+
"go.sum",
12+
"**/*test.go"
13+
]
14+
},
15+
"context": {
16+
"@aws-cdk/aws-apigateway:usagePlanKeyOrderInsensitiveId": true,
17+
"@aws-cdk/core:stackRelativeExports": true,
18+
"@aws-cdk/aws-rds:lowercaseDbIdentifier": true,
19+
"@aws-cdk/aws-lambda:recognizeVersionProps": true,
20+
"@aws-cdk/aws-cloudfront:defaultSecurityPolicyTLSv1.2_2021": true
21+
}
22+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
package main
2+
3+
import (
4+
"github.com/aws/aws-cdk-go/awscdk/v2"
5+
dynamodb "github.com/aws/aws-cdk-go/awscdk/v2/awsdynamodb"
6+
"github.com/aws/constructs-go/constructs/v10"
7+
"github.com/aws/aws-sdk-go-v2/aws"
8+
)
9+
10+
type DynamodbCdkGoStackProps struct {
11+
awscdk.StackProps
12+
}
13+
14+
func NewDynamodbCdkGoStack(scope constructs.Construct, id string, props *DynamodbCdkGoStackProps) awscdk.Stack {
15+
var sprops awscdk.StackProps
16+
if props != nil {
17+
sprops = props.StackProps
18+
}
19+
stack := awscdk.NewStack(scope, &id, &sprops)
20+
this := stack
21+
table := dynamodb.NewTable(this, aws.String("table"), &dynamodb.TableProps{
22+
PartitionKey: &dynamodb.Attribute{
23+
Name: aws.String("username"),
24+
Type: dynamodb.AttributeType_STRING,
25+
},
26+
BillingMode: dynamodb.BillingMode_PAY_PER_REQUEST,
27+
TableName: aws.String("Username"),
28+
})
29+
30+
awscdk.Tags_Of(table).Add(aws.String("Name"), aws.String("Username"),nil)
31+
32+
return stack
33+
}
34+
35+
func main() {
36+
app := awscdk.NewApp(nil)
37+
38+
NewDynamodbCdkGoStack(app, "DynamodbCdkGoStack", &DynamodbCdkGoStackProps{
39+
awscdk.StackProps{
40+
Env: env(),
41+
},
42+
})
43+
44+
app.Synth(nil)
45+
}
46+
47+
// env determines the AWS environment (account+region) in which our stack is to
48+
// be deployed. For more information see: https://docs.aws.amazon.com/cdk/latest/guide/environments.html
49+
func env() *awscdk.Environment {
50+
// If unspecified, this stack will be "environment-agnostic".
51+
// Account/Region-dependent features and context lookups will not work, but a
52+
// single synthesized template can be deployed anywhere.
53+
//---------------------------------------------------------------------------
54+
return nil
55+
56+
// Uncomment if you know exactly what account and region you want to deploy
57+
// the stack to. This is the recommendation for production stacks.
58+
//---------------------------------------------------------------------------
59+
// return &awscdk.Environment{
60+
// Account: jsii.String("123456789012"),
61+
// Region: jsii.String("us-east-1"),
62+
// }
63+
64+
// Uncomment to specialize this stack for the AWS Account and Region that are
65+
// implied by the current CLI configuration. This is recommended for dev
66+
// stacks.
67+
//---------------------------------------------------------------------------
68+
// return &awscdk.Environment{
69+
// Account: jsii.String(os.Getenv("CDK_DEFAULT_ACCOUNT")),
70+
// Region: jsii.String(os.Getenv("CDK_DEFAULT_REGION")),
71+
// }
72+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package main
2+
3+
import (
4+
"testing"
5+
6+
"github.com/aws/aws-cdk-go/awscdk/v2"
7+
assertions "github.com/aws/aws-cdk-go/awscdk/v2/assertions"
8+
"github.com/aws/aws-sdk-go-v2/aws"
9+
)
10+
11+
// example tests. To run these tests, uncomment this file along with the
12+
// example resource in dynamodb-cdk-go_test.go
13+
func TestDynamodbCdkGoStack(t *testing.T) {
14+
// GIVEN
15+
app := awscdk.NewApp(nil)
16+
17+
// WHEN
18+
stack := NewDynamodbCdkGoStack(app, "MyStack", nil)
19+
20+
// THEN
21+
template := assertions.Template_FromStack(stack)
22+
23+
template.HasResourceProperties(aws.String("AWS::DynamoDB::Table"), map[string]interface{}{
24+
"TableName": "Username",
25+
})
26+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
module dynamodb-cdk-go
2+
3+
go 1.16
4+
5+
require (
6+
github.com/aws/aws-cdk-go/awscdk/v2 v2.3.0
7+
github.com/aws/aws-sdk-go-v2 v1.11.2
8+
github.com/aws/constructs-go/constructs/v10 v10.0.9
9+
)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
github.com/Masterminds/semver/v3 v3.1.1 h1:hLg3sBzpNErnxhQtUy/mmLR2I9foDujNK030IGemrRc=
2+
github.com/Masterminds/semver/v3 v3.1.1/go.mod h1:VPu/7SZ7ePZ3QOrcuXROw5FAcLl4a0cBrbBpGY/8hQs=
3+
github.com/aws/aws-cdk-go/awscdk/v2 v2.3.0 h1:eSV6tyCLzwYQcrwrJX1TV1c7AD3qvvA1jX86u8K08ro=
4+
github.com/aws/aws-cdk-go/awscdk/v2 v2.3.0/go.mod h1:HBrUWuxQB3EBqdLT2pD+5YrVHOf0UkE6g0uQqIfKlW0=
5+
github.com/aws/aws-sdk-go-v2 v1.11.2 h1:SDiCYqxdIYi6HgQfAWRhgdZrdnOuGyLDJVRSWLeHWvs=
6+
github.com/aws/aws-sdk-go-v2 v1.11.2/go.mod h1:SQfA+m2ltnu1cA0soUkj4dRSsmITiVQUJvBIZjzfPyQ=
7+
github.com/aws/constructs-go/constructs/v10 v10.0.9 h1:YGk+deTAD3rgyANybjOtaoVrjC7HZZtALeC872avWFQ=
8+
github.com/aws/constructs-go/constructs/v10 v10.0.9/go.mod h1:RC6w8bOwxLmPX7Jfo9dkEZ9iVfgH4QnaVnfWvaNOHy0=
9+
github.com/aws/jsii-runtime-go v1.37.0/go.mod h1:6tZnlstx8bAB3vnLFF9n8bbkI//LDblAek9zFyMXV3E=
10+
github.com/aws/jsii-runtime-go v1.47.0 h1:iaL6yPegP9+yBIARJdccy0QjmvjH5y9eu6nSbpEgZE4=
11+
github.com/aws/jsii-runtime-go v1.47.0/go.mod h1:6tZnlstx8bAB3vnLFF9n8bbkI//LDblAek9zFyMXV3E=
12+
github.com/aws/smithy-go v1.9.0 h1:c7FUdEqrQA1/UVKKCNDFQPNKGp4FQg3YW4Ck5SLTG58=
13+
github.com/aws/smithy-go v1.9.0/go.mod h1:SObp3lf9smib00L/v3U2eAKG8FyQ7iLrJnQiAmR5n+E=
14+
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
15+
github.com/google/go-cmp v0.5.4/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
16+
github.com/google/go-cmp v0.5.6 h1:BKbKCqvP6I+rmFHt06ZmyQtvB8xAkWdhFyr0ZUNZcxQ=
17+
github.com/google/go-cmp v0.5.6/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
18+
github.com/jmespath/go-jmespath v0.4.0/go.mod h1:T8mJZnbsbmF+m6zOOFylbeCJqk5+pHWvzYPziyZiYoo=
19+
github.com/jmespath/go-jmespath/internal/testify v1.5.1/go.mod h1:L3OGu8Wl2/fWfCI6z80xFu9LTZmf1ZRjMHUOPmWr69U=
20+
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
21+
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
22+
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
23+
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
24+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
25+
gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
26+
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
*.js
2+
!jest.config.js
3+
*.d.ts
4+
node_modules
5+
6+
# CDK asset staging directory
7+
.cdk.staging
8+
cdk.out
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
*.ts
2+
!*.d.ts
3+
4+
# CDK asset staging directory
5+
.cdk.staging
6+
cdk.out
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# Create DynamoDB Table with CDK Typescript
2+
3+
See [go-on-aws](https://www.go-on-aws.com/goformation/) for details.
4+
5+
To compare goformation.
6+
7+
## Init
8+
9+
```bash
10+
npx [email protected] init app -l=typescript 17,54s user 6,81s system 100% cpu 24,162 total
11+
```
12+
13+
## Synth
14+
15+
```bash
16+
npx [email protected] ls 6,85s user 1,85s system 55% cpu 15,807 total
17+
```
18+
19+
Without npx
20+
21+
```bash
22+
cdk ls 3,88s user 0,27s system 140% cpu 2,949 total
23+
```
24+
25+
26+
```bash
27+
cdk synth 3,87s user 0,27s system 140% cpu 2,944 total
28+
```
29+
30+
31+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#!/usr/bin/env node
2+
import 'source-map-support/register';
3+
import * as cdk from 'aws-cdk-lib';
4+
import { DynamodbcdkStack } from '../lib/dynamodbcdk-stack';
5+
6+
const app = new cdk.App();
7+
new DynamodbcdkStack(app, 'DynamodbcdkStack', {
8+
/* If you don't specify 'env', this stack will be environment-agnostic.
9+
* Account/Region-dependent features and context lookups will not work,
10+
* but a single synthesized template can be deployed anywhere. */
11+
12+
/* Uncomment the next line to specialize this stack for the AWS Account
13+
* and Region that are implied by the current CLI configuration. */
14+
// env: { account: process.env.CDK_DEFAULT_ACCOUNT, region: process.env.CDK_DEFAULT_REGION },
15+
16+
/* Uncomment the next line if you know exactly what Account and Region you
17+
* want to deploy the stack to. */
18+
// env: { account: '123456789012', region: 'us-east-1' },
19+
20+
/* For more information, see https://docs.aws.amazon.com/cdk/latest/guide/environments.html */
21+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
{
2+
"app": "npx ts-node --prefer-ts-exts bin/dynamodbcdk.ts",
3+
"watch": {
4+
"include": [
5+
"**"
6+
],
7+
"exclude": [
8+
"README.md",
9+
"cdk*.json",
10+
"**/*.d.ts",
11+
"**/*.js",
12+
"tsconfig.json",
13+
"package*.json",
14+
"yarn.lock",
15+
"node_modules",
16+
"test"
17+
]
18+
},
19+
"context": {
20+
"@aws-cdk/aws-apigateway:usagePlanKeyOrderInsensitiveId": true,
21+
"@aws-cdk/core:stackRelativeExports": true,
22+
"@aws-cdk/aws-rds:lowercaseDbIdentifier": true,
23+
"@aws-cdk/aws-lambda:recognizeVersionProps": true,
24+
"@aws-cdk/aws-cloudfront:defaultSecurityPolicyTLSv1.2_2021": true
25+
}
26+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
module.exports = {
2+
testEnvironment: 'node',
3+
roots: ['<rootDir>/test'],
4+
testMatch: ['**/*.test.ts'],
5+
transform: {
6+
'^.+\\.tsx?$': 'ts-jest'
7+
}
8+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { aws_dynamodb as dynamodb, Stack, StackProps, Tags } from 'aws-cdk-lib';
2+
import { Construct } from 'constructs';
3+
4+
export class DynamodbcdkStack extends Stack {
5+
constructor(scope: Construct, id: string, props?: StackProps) {
6+
super(scope, id, props);
7+
8+
// The code that defines your stack goes here
9+
10+
// example resource
11+
const table = new dynamodb.Table( this, 'table',{
12+
partitionKey: { name: 'Username', type: dynamodb.AttributeType.STRING },
13+
billingMode: dynamodb.BillingMode.PAY_PER_REQUEST,
14+
tableName: "UserTableCDK",
15+
})
16+
17+
Tags.of(table).add('Name','Username')
18+
}
19+
}

0 commit comments

Comments
 (0)