Skip to content

Commit

Permalink
Merge branch 'new-terraform' into version-3
Browse files Browse the repository at this point in the history
# Conflicts:
#	demo/terraform/example/.terraform.lock.hcl
#	demo/terraform/example/scheduletestmodelrestcleanDumps.tf
#	demo/terraform/lkec2/cloud.tf
#	demo/terraform/lkec2/database.tf
#	demo/terraform/lkec2/email.tf
#	demo/terraform/lkec2/files.tf
#	demo/terraform/lkec2/general.tf
#	demo/terraform/lkec2/main.tf
#	demo/terraform/lkec2/metrics.tf
#	demo/terraform/lkec2/project.json
#	demo/terraform/lkec2/terraform.tfvars
#	demo/terraform/lkec2/tf
#	demo/terraform/lkec2/tf.ps1
#	server-core/src/test/kotlin/com/lightningkite/lightningserver/serialization/CSV2Test.kt
#	server-mongo/src/test/kotlin/com/lightningkite/lightningdb/SearchTextConditionTest.kt
  • Loading branch information
UnknownJoe796 committed Oct 28, 2024
2 parents 3affe88 + 281391d commit b73260c
Show file tree
Hide file tree
Showing 54 changed files with 4,074 additions and 3,015 deletions.
2 changes: 2 additions & 0 deletions demo/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ tasks.create("rebuildTerraform", JavaExec::class.java) {
mainClass.set("com.lightningkite.lightningserverdemo.MainKt")
args("terraform")
workingDir(project.rootDir)
inputs.files(*file("terraform").walkTopDown().filter { it.name == "project.json" }.toList().toTypedArray())
}

fun env(name: String, profile: String) {
Expand Down Expand Up @@ -94,6 +95,7 @@ fun env(name: String, profile: String) {
}
}
env("example", "default")
env("lkec2", "lk")

tasks.create("proguardTest", ProGuardTask::class) {
this.injars(tasks.getByName("shadowJar"))
Expand Down
4 changes: 4 additions & 0 deletions demo/src/main/kotlin/Server.kt
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,10 @@ object Server : ServerPathGroup(ServerPath.root) {
HttpResponse.plainText("Hello ${it.user<User?>()}")
}

val die2 = path("oom").get.handler {
throw OutOfMemoryError("Fake")
}

val socket = path("socket").websocket(
connect = { println("Connected $it - you are ${it.user<User?>()}") },
message = {
Expand Down
17 changes: 3 additions & 14 deletions demo/src/main/kotlin/main.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
package com.lightningkite.lightningserverdemo

import com.lightningkite.kotlinercli.cli
import com.lightningkite.lightningserver.aws.terraformMigrate
import com.lightningkite.lightningserver.aws.terraformAws
import com.lightningkite.lightningserver.cache.*
import com.lightningkite.lightningserver.files.ServerFile
import com.lightningkite.lightningserver.ktor.runServer
Expand All @@ -16,6 +14,7 @@ import java.io.File
import kotlinx.datetime.Instant
import java.util.*
import com.lightningkite.UUID
import com.lightningkite.lightningserver.aws.terraform.createTerraform

fun setup() {
Server
Expand All @@ -28,23 +27,13 @@ private fun serve() {

fun terraform() {
Server
terraformAws("com.lightningkite.lightningserverdemo.AwsHandler", "demo", File("demo/terraform2"))
}

fun tfMigrate() {
Server
terraformMigrate("com.lightningkite.lightningserverdemo.AwsHandler", File("demo/terraform"))
createTerraform("com.lightningkite.lightningserverdemo.AwsHandler", "demo", File("demo/terraform"))
}

fun main(vararg args: String) {
cli(
arguments = args,
setup = ::setup,
available = listOf(::serve, ::terraform, ::tfMigrate, ::dbTest),
available = listOf(::serve, ::terraform),
)
}

fun dbTest(): Unit = runBlocking {
Server
loadSettings(File("settings.json"))
}
112 changes: 0 additions & 112 deletions demo/terraform/example/alarms.tf

This file was deleted.

53 changes: 4 additions & 49 deletions demo/terraform/example/cloud.tf
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@ variable "deployment_location" {
type = string
default = "us-west-2"
nullable = false
description = "The AWS region key to deploy all resources in."
}
variable "debug" {
type = bool
default = false
nullable = false
description = "The GeneralSettings debug. Debug true will turn on various things during run time for easier development and bug tracking. Should be false for production environments."
}
variable "ip_prefix" {
type = string
Expand All @@ -21,10 +23,12 @@ variable "ip_prefix" {
variable "domain_name_zone" {
type = string
nullable = false
description = "The AWS Hosted zone the domain will be placed under."
}
variable "domain_name" {
type = string
nullable = false
description = "The domain the server will be hosted at."
}

##########
Expand All @@ -36,55 +40,6 @@ variable "domain_name" {
# Resources
##########


resource "aws_api_gateway_account" "main" {
cloudwatch_role_arn = aws_iam_role.cloudwatch.arn
}

resource "aws_iam_role" "cloudwatch" {
name = "demoexample"

assume_role_policy = <<EOF
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "",
"Effect": "Allow",
"Principal": {
"Service": ["apigateway.amazonaws.com", "lambda.amazonaws.com"]
},
"Action": "sts:AssumeRole"
}
]
}
EOF
}
resource "aws_iam_role_policy" "cloudwatch" {
name = "demoexample_policy"
role = aws_iam_role.cloudwatch.id

policy = <<EOF
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"logs:CreateLogGroup",
"logs:CreateLogStream",
"logs:DescribeLogGroups",
"logs:DescribeLogStreams",
"logs:PutLogEvents",
"logs:GetLogEvents",
"logs:FilterLogEvents"
],
"Resource": "*"
}
]
}
EOF
}
data "aws_route53_zone" "main" {
name = var.domain_name_zone
}
Expand Down
4 changes: 0 additions & 4 deletions demo/terraform/example/files.tf
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,4 @@ resource "aws_iam_policy" "files" {
]
})
}
resource "aws_iam_role_policy_attachment" "files" {
role = aws_iam_role.main_exec.name
policy_arn = aws_iam_policy.files.arn
}

2 changes: 2 additions & 0 deletions demo/terraform/example/general.tf
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@ variable "cors" {
type = object({ allowedDomains = list(string), allowedHeaders = list(string) })
default = null
nullable = true
description = "Defines the cors rules for the server."
}
variable "display_name" {
type = string
default = "demo-example"
nullable = false
description = "The GeneralSettings projectName."
}

##########
Expand Down
31 changes: 28 additions & 3 deletions demo/terraform/example/lambda.tf
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,19 @@ variable "lambda_memory_size" {
type = number
default = 1024
nullable = false
description = "The amount of ram available (in Megabytes) to the virtual machine running in Lambda."
}
variable "lambda_timeout" {
type = number
default = 30
nullable = false
description = "How long an individual lambda invocation can run before forcefully being shut down."
}
variable "lambda_snapstart" {
type = bool
default = false
nullable = false
description = "Whether or not lambda will deploy with SnapStart which compromises deploy time for shorter cold start time."
}

##########
Expand Down Expand Up @@ -102,6 +105,18 @@ resource "aws_iam_role_policy_attachment" "insights_policy" {
policy_arn = "arn:aws:iam::aws:policy/CloudWatchLambdaInsightsExecutionRolePolicy"
}

resource "aws_iam_role_policy_attachment" "files" {
role = aws_iam_role.main_exec.id
policy_arn = aws_iam_policy.files.arn
}


resource "aws_iam_role_policy_attachment" "metrics" {
role = aws_iam_role.main_exec.id
policy_arn = aws_iam_policy.metrics.arn
}


resource "aws_s3_object" "app_storage" {
bucket = aws_s3_bucket.lambda_bucket.id

Expand All @@ -120,7 +135,7 @@ resource "aws_lambda_function" "main" {
s3_key = aws_s3_object.app_storage.key

runtime = "java17"
handler = "com.lightningkite.lightningserver.demo.AwsHandler"
handler = "com.lightningkite.lightningserverdemo.AwsHandler"

memory_size = "${var.lambda_memory_size}"
timeout = var.lambda_timeout
Expand Down Expand Up @@ -205,11 +220,11 @@ resource "null_resource" "lambda_jar_source" {
always = timestamp()
}
provisioner "local-exec" {
command = local.is_windows ? "if(test-path \"${path.module}/build/lambda/\") { rd -Recurse \"${path.module}/build/lambda/\" }" : "rm -rf \"${path.module}/build/lambda/\""
command = (local.is_windows ? "if(test-path \"${path.module}/build/lambda/\") { rd -Recurse \"${path.module}/build/lambda/\" }" : "rm -rf \"${path.module}/build/lambda/\"")
interpreter = local.is_windows ? ["PowerShell", "-Command"] : []
}
provisioner "local-exec" {
command = local.is_windows ? "cp -r -force \"${path.module}/../../build/dist/lambda/.\" \"${path.module}/build/lambda/\"" : "cp -rf \"${path.module}/../../build/dist/lambda/.\" \"${path.module}/build/lambda/\""
command = (local.is_windows ? "cp -r -force \"${path.module}/../../build/dist/lambda/.\" \"${path.module}/build/lambda/\"" : "cp -rf \"${path.module}/../../build/dist/lambda/.\" \"${path.module}/build/lambda/\"")
interpreter = local.is_windows ? ["PowerShell", "-Command"] : []
}
provisioner "local-exec" {
Expand Down Expand Up @@ -241,5 +256,15 @@ data "archive_file" "lambda" {
output_path = "${path.module}/build/lambda.jar"
}

data "aws_caller_identity" "current" {}
resource "aws_lambda_permission" "scheduled_tasks" {
action = "lambda:InvokeFunction"
function_name = "${aws_lambda_alias.main.function_name}:${aws_lambda_alias.main.name}"
principal = "events.amazonaws.com"
source_arn = "arn:aws:events:${var.deployment_location}:${data.aws_caller_identity.current.account_id}:rule/demo-example_*"
lifecycle {
create_before_destroy = false
}
}


Loading

0 comments on commit b73260c

Please sign in to comment.