-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgo-aws.go
More file actions
98 lines (78 loc) · 3.17 KB
/
Copy pathgo-aws.go
File metadata and controls
98 lines (78 loc) · 3.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package main
import (
"github.com/aws/aws-cdk-go/awscdk/v2"
"github.com/aws/aws-cdk-go/awscdk/v2/awsapigateway"
"github.com/aws/aws-cdk-go/awscdk/v2/awsdynamodb"
"github.com/aws/aws-cdk-go/awscdk/v2/awslambda"
"github.com/aws/constructs-go/constructs/v10"
"github.com/aws/jsii-runtime-go"
)
type GoAwsStackProps struct {
awscdk.StackProps
}
func NewGoAwsStack(scope constructs.Construct, id string, props *GoAwsStackProps) awscdk.Stack {
var sprops awscdk.StackProps
if props != nil {
sprops = props.StackProps
}
stack := awscdk.NewStack(scope, &id, &sprops)
//* Create DB table
table := awsdynamodb.NewTable(stack, jsii.String("myUserTable"), &awsdynamodb.TableProps{
PartitionKey: &awsdynamodb.Attribute{
Name: jsii.String("username"), // should be the same as primary key
Type: awsdynamodb.AttributeType_STRING,
},
TableName: jsii.String("userTable"), // should be the same as TABLE_NAME
RemovalPolicy: awscdk.RemovalPolicy_DESTROY, // remove the DB when the stack is destroyed(cdk destroy)
})
//* Create a lambda function with Docker Image
myFunction := awslambda.NewDockerImageFunction(stack, jsii.String("myLambdaFunction"), &awslambda.DockerImageFunctionProps{
//* use a Docker image for the lambda function
Code: awslambda.DockerImageCode_FromImageAsset(jsii.String("./lambda"), nil),
//* use ARM64 architecture for better performance and cost
//* need to specify the architecture because linux can operate on both x86 and ARM
Architecture: awslambda.Architecture_X86_64(),
Tracing: awslambda.Tracing_ACTIVE, //* enable tracing for the lambda function
})
//* Grant the lambda function read/write access to the table
//* Doing this to connect the lambda function to the table
table.GrantReadWriteData(myFunction)
//* Create an API Gateway
api := awsapigateway.NewRestApi(stack, jsii.String("myAPIGateway"), &awsapigateway.RestApiProps{
//* Enable CORS
DefaultCorsPreflightOptions: &awsapigateway.CorsOptions{
AllowHeaders: jsii.Strings("Content-Type", "Authorization"), // headers
AllowMethods: jsii.Strings("GET", "POST", "DELETE", "PUT", "OPTIONS"), // methods
AllowOrigins: jsii.Strings("*"), // origins
},
DeployOptions: &awsapigateway.StageOptions{
LoggingLevel: awsapigateway.MethodLoggingLevel_INFO,
},
CloudWatchRole: jsii.Bool(true), // fix cloudwatch error on deployment
})
integration := awsapigateway.NewLambdaIntegration(myFunction, nil)
//* Define the routes
//* Register route
registerResource := api.Root().AddResource(jsii.String("register"), nil)
registerResource.AddMethod(jsii.String("POST"), integration, nil)
//* Login route
loginResource := api.Root().AddResource(jsii.String("login"), nil)
loginResource.AddMethod(jsii.String("POST"), integration, nil)
//* Protected route
protectedResource := api.Root().AddResource(jsii.String("protected"), nil)
protectedResource.AddMethod(jsii.String("GET"), integration, nil)
return stack
}
func main() {
defer jsii.Close()
app := awscdk.NewApp(nil)
NewGoAwsStack(app, "AuthStack", &GoAwsStackProps{
awscdk.StackProps{
Env: env(),
},
})
app.Synth(nil)
}
func env() *awscdk.Environment {
return nil
}