-
Notifications
You must be signed in to change notification settings - Fork 0
/
fems.go
85 lines (67 loc) · 2.42 KB
/
fems.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 FemsStackProps struct {
awscdk.StackProps
}
func NewFemsStack(scope constructs.Construct, id string, props *FemsStackProps) awscdk.Stack {
var sprops awscdk.StackProps
if props != nil {
sprops = props.StackProps
}
stack := awscdk.NewStack(scope, &id, &sprops)
// create db table here
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.AssetCode_FromAsset(jsii.String("lambda/function.zip"), nil),
Handler: jsii.String("main"),
})
table.GrantReadWriteData(myFunction)
api := awsapigateway.NewRestApi(stack, jsii.String("myAPIGateway"), &awsapigateway.RestApiProps{
DefaultCorsPreflightOptions: &awsapigateway.CorsOptions{
AllowHeaders: jsii.Strings("Content-Type", "Authorization"),
AllowMethods: jsii.Strings("GET", "POST", "DELETE", "PUT", "OPTIONS"),
AllowOrigins: jsii.Strings("*"),
},
DeployOptions: &awsapigateway.StageOptions{
LoggingLevel: awsapigateway.MethodLoggingLevel_INFO,
},
CloudWatchRole: jsii.Bool(true),
})
integration := awsapigateway.NewLambdaIntegration(myFunction, nil)
registerResource := api.Root().AddResource(jsii.String("register"), nil)
registerResource.AddMethod(jsii.String("POST"), integration, nil)
loginResource := api.Root().AddResource(jsii.String("login"), nil)
loginResource.AddMethod(jsii.String("POST"), integration, nil)
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)
NewFemsStack(app, "FemsStack", &FemsStackProps{
awscdk.StackProps{
Env: env(),
},
})
app.Synth(nil)
}
func env() *awscdk.Environment {
return nil
}