From e972863be6d54b8dc64b1a7209392d5af7cb80fd Mon Sep 17 00:00:00 2001 From: Sam Alba Date: Mon, 12 Apr 2021 16:19:15 -0700 Subject: [PATCH] examples/jamstack: implemented database integration with RDS Signed-off-by: Sam Alba --- examples/jamstack/backend.cue | 21 ----------------- examples/jamstack/database.cue | 41 ++++++++++++++++++++++++++++++++++ examples/jamstack/ecs.cue | 2 +- examples/jamstack/infra.cue | 31 +++++++++++++++++++++++++ examples/jamstack/main.cue | 18 +++++++++++++++ 5 files changed, 91 insertions(+), 22 deletions(-) create mode 100644 examples/jamstack/database.cue create mode 100644 examples/jamstack/infra.cue diff --git a/examples/jamstack/backend.cue b/examples/jamstack/backend.cue index 39fdbaf1..40458217 100644 --- a/examples/jamstack/backend.cue +++ b/examples/jamstack/backend.cue @@ -2,30 +2,9 @@ package main import ( "dagger.io/dagger" - "dagger.io/aws" "dagger.io/aws/ecs" ) -infra: { - // AWS auth & default region - awsConfig: aws.#Config - - // VPC Id - vpcId: string - - // ECR Image repository - ecrRepository: string - - // ECS cluster name - ecsClusterName: string - - // Execution Role ARN used for all tasks running on the cluster - ecsTaskRoleArn?: string - - // ELB listener ARN - elbListenerArn: string -} - // Backend configuration backend: { // Source code to build this container diff --git a/examples/jamstack/database.cue b/examples/jamstack/database.cue new file mode 100644 index 00000000..93edbe43 --- /dev/null +++ b/examples/jamstack/database.cue @@ -0,0 +1,41 @@ +package main + +import ( + "encoding/base64" + "dagger.io/aws/rds" +) + +database: { + let slug = name + dbType: "mysql" + + db: rds.#CreateDB & { + config: infra.awsConfig + name: slug + dbArn: infra.rdsInstanceArn + "dbType": dbType + secretArn: infra.rdsAdminSecretArn + } + + user: rds.#CreateUser & { + config: infra.awsConfig + dbArn: infra.rdsInstanceArn + "dbType": dbType + secretArn: infra.rdsAdminSecretArn + username: slug + // FIXME: make it secure (generate infra side?) + password: base64.Encode(null, "pwd-\(slug)") + grantDatabase: db.out + } + + instance: rds.#Instance & { + config: infra.awsConfig + dbArn: infra.rdsInstanceArn + } + + hostname: instance.hostname + port: instance.port + dbName: db.out + username: user.out + password: user.password +} diff --git a/examples/jamstack/ecs.cue b/examples/jamstack/ecs.cue index 1f018ecf..e496c8ac 100644 --- a/examples/jamstack/ecs.cue +++ b/examples/jamstack/ecs.cue @@ -53,7 +53,7 @@ import ( template: { AWSTemplateFormatVersion: "2010-09-09" - Description: "Blocklayer deployed app" + Description: "Dagger deployed app" Parameters: { ELBRulePriority: Type: "Number" ImageRef: Type: "String" diff --git a/examples/jamstack/infra.cue b/examples/jamstack/infra.cue new file mode 100644 index 00000000..e0c3bd07 --- /dev/null +++ b/examples/jamstack/infra.cue @@ -0,0 +1,31 @@ +package main + +import ( + "dagger.io/aws" +) + +infra: { + // AWS auth & default region + awsConfig: aws.#Config + + // VPC Id + vpcId: string + + // ECR Image repository + ecrRepository: string + + // ECS cluster name + ecsClusterName: string + + // Execution Role ARN used for all tasks running on the cluster + ecsTaskRoleArn?: string + + // ELB listener ARN + elbListenerArn: string + + // Secret ARN for the admin password of the RDS Instance + rdsAdminSecretArn: string + + // ARN of the RDS Instance + rdsInstanceArn: string +} diff --git a/examples/jamstack/main.cue b/examples/jamstack/main.cue index aecb750f..e495aa2b 100644 --- a/examples/jamstack/main.cue +++ b/examples/jamstack/main.cue @@ -2,3 +2,21 @@ package main // Name of the application name: string & =~"[a-z0-9-]+" + +// FIXME: temporary workaround (GH issue #142) - image metadata is lost after build +backend: container: command: ["/bin/hello-go"] + +// Inject db info in the container environment +backend: environment: { + DB_USERNAME: database.username + DB_HOSTNAME: database.hostname + DB_PASSWORD: database.password + DB_DBNAME: database.dbName + DB_PORT: "\(database.port)" + DB_TYPE: database.dbType +} + +url: { + frontendURL: "FIXME" + backendURL: "https://\(backend.hostname)/" +}