examples/jamstack: implemented database integration with RDS

Signed-off-by: Sam Alba <sam.alba@gmail.com>
This commit is contained in:
Sam Alba 2021-04-12 16:19:15 -07:00
parent 0b3395b9e8
commit e972863be6
5 changed files with 91 additions and 22 deletions

View File

@ -2,30 +2,9 @@ package main
import ( import (
"dagger.io/dagger" "dagger.io/dagger"
"dagger.io/aws"
"dagger.io/aws/ecs" "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 configuration
backend: { backend: {
// Source code to build this container // Source code to build this container

View File

@ -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
}

View File

@ -53,7 +53,7 @@ import (
template: { template: {
AWSTemplateFormatVersion: "2010-09-09" AWSTemplateFormatVersion: "2010-09-09"
Description: "Blocklayer deployed app" Description: "Dagger deployed app"
Parameters: { Parameters: {
ELBRulePriority: Type: "Number" ELBRulePriority: Type: "Number"
ImageRef: Type: "String" ImageRef: Type: "String"

View File

@ -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
}

View File

@ -2,3 +2,21 @@ package main
// Name of the application // Name of the application
name: string & =~"[a-z0-9-]+" 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)/"
}