-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgo_cdk.go
85 lines (73 loc) · 2.74 KB
/
go_cdk.go
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
package main
import (
"github.com/aws/aws-cdk-go/awscdk/v2"
// "github.com/aws/aws-cdk-go/awscdk/v2/awssqs"
"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 GoCdkStackProps struct {
awscdk.StackProps
}
func NewGoCdkStack(scope constructs.Construct, id string, props *GoCdkStackProps) awscdk.Stack {
var sprops awscdk.StackProps
if props != nil {
sprops = props.StackProps
}
stack := awscdk.NewStack(scope, &id, &sprops)
// make a new table
table := awsdynamodb.NewTable(stack, jsii.String("myUserTable"), &awsdynamodb.TableProps{
PartitionKey: &awsdynamodb.Attribute{
Name: jsii.String("username"),
Type: awsdynamodb.AttributeType_STRING,
},
TableName: jsii.String("userTable"),
RemovalPolicy: awscdk.RemovalPolicy_DESTROY,
})
myFunction := awslambda.NewFunction(stack, jsii.String("myLambdaFunction"), &awslambda.FunctionProps{
Runtime: awslambda.Runtime_PROVIDED_AL2023(),
Code: awslambda.Code_FromAsset(jsii.String("lambda/function.zip"), nil),
Handler: jsii.String("main"),
})
// grant lambda permissions to read/write to the table
table.GrantReadWriteData(myFunction)
// create api gateway with cors enabled
api := awsapigateway.NewRestApi(stack, jsii.String("myApiGateway"), &awsapigateway.RestApiProps{
DefaultCorsPreflightOptions: &awsapigateway.CorsOptions{
AllowHeaders: jsii.Strings("Content-Type", "Authorization"),
AllowMethods: jsii.Strings("OPTIONS", "GET", "POST", "PUT", "DELETE"),
AllowOrigins: jsii.Strings("*"),
},
DeployOptions: &awsapigateway.StageOptions{
LoggingLevel: awsapigateway.MethodLoggingLevel_INFO,
},
CloudWatchRole: jsii.Bool(true),
})
// integrate the lambda function with the api gateway
integration := awsapigateway.NewLambdaIntegration(myFunction, nil)
// Define Routes
registerResource := api.Root().AddResource(jsii.String("register"), nil) // its like defining localhost:3000/register
registerResource.AddMethod(jsii.String("POST"), integration, nil)
// Define Routes
loginResource := api.Root().AddResource(jsii.String("login"), nil) // its like defining localhost:3000/register
loginResource.AddMethod(jsii.String("POST"), integration, nil)
// Define Routes
protectedResource := api.Root().AddResource(jsii.String("protected"), nil) // its like defining localhost:3000/register
protectedResource.AddMethod(jsii.String("GET"), integration, nil)
return stack
}
func main() {
defer jsii.Close()
app := awscdk.NewApp(nil)
NewGoCdkStack(app, "GoCdkStack", &GoCdkStackProps{
awscdk.StackProps{
Env: env(),
},
})
app.Synth(nil)
}
func env() *awscdk.Environment {
return nil
}