Skip to content

Code Samples#

Here you'll find code sample on how to programmatically connect and use your resource.

NodeJs#

You'll need to have node installed and then open up a terminal.

Setup your environment#

mkdir ametnes-nodejs-sample && cd ametnes-nodejs-sample
npm install mysql

Connect and run some queries#

In the terminal, start the nodejs command prompt.

$node
Welcome to Node.js v15.5.0.
Type ".help" for more information.
In the nodejs command prompt, enter the following commands. Note that the connection properties can be obtained from the Ametnes Cloud console.
> var mysql = require('mysql');
> var connection = mysql.createConnection({host: 'host', user: 'user', password: 'password', database: 'database', ssl : true});
> connection.connect();
> connection.query("SHOW SESSION STATUS LIKE 'Ssl_cipher';", function (error, results, fields) {
  if (error) throw error;
  console.log(results);
});
> [
  RowDataPacket {
    Variable_name: 'Ssl_cipher',
    Value: 'ECDHE-RSA-AES128-GCM-SHA256'
  }
]

The result

[
  RowDataPacket {
    Variable_name: 'Ssl_cipher',
    Value: 'ECDHE-RSA-AES128-GCM-SHA256'
  }
]

Indicates that your connection is secure.

Terraform#

In this sample code, we will create some AWS resources using Terraform and an Ametnes Postgres Service for our backend. We will then perform some SQL queries on the terraform state. Our architecture is a simple multi-region VPC setup.

Setup your environment#

Prerequisites#

  1. Firstly create an Ametnes Postgres Service. For this example, we will assume an Ametnes Postgres Service provisioned in Ametnes location aws/eu-west-2.
  2. Then, make sure you have Terraform installed on your workstation.
  3. You'll need to have an AWS account and your local environment setup correctly for AWS.

Environment Setup#

Create the backend database to store the terraform state.

$mkdir ametnes-terraform-sample && cd ametnes-terraform-sample
$pgcli -h postgres-a6bf.euw2.aws.cloud.ametnes.com -p 28427 -u somerandomuza initdb
$Password: <enter here>
initdb> create database networks

Sample Code#

Create the file ametnes-terraform-sample/main.tf and paste this code in it.

terraform {
  backend "pg" {
    conn_str = "postgres://somerandomuza:TheyaPa55Word@postgres-a6bf.euw2.aws.cloud.ametnes.com:28427/networks"
    schema_name = "tfstate"
  }
}

provider "aws" {
  alias = "euw1"
  region = "eu-west-1"
}

provider "aws" {
  alias = "euc1"
  region = "eu-central-1"
}

module "vpc_euc1" {
  providers = {
    "aws" = aws.euc1
  }
  source  = "terraform-aws-modules/vpc/aws"
  version = "2.64.0"
  name = "ametnes-demo-vpc"

  cidr = "10.0.0.0/16"

  azs             = ["eu-central-1a", "eu-central-1b"]
  private_subnets = ["10.0.1.0/24", "10.0.2.0/24"]
  public_subnets  = ["10.0.101.0/24", "10.0.102.0/24"]

  enable_ipv6 = true

  enable_nat_gateway = false
  single_nat_gateway = false

  public_subnet_tags = {
    Name = "overridden-name-public"
  }

  tags = {
    Owner       = "Ametnes Devops"
    Environment = "Development"
    Location = "Frankfurt"
  }

  vpc_tags = {
    Name = "demo-vpc"
  }
}

module "vpc_euw1" {
  providers = {
    "aws" = aws.euw1
  }
  source  = "terraform-aws-modules/vpc/aws"
  version = "2.64.0"
  name = "ametnes-demo-vpc"

  cidr = "10.1.0.0/16"

  azs             = ["eu-west-1a", "eu-west-1b", "euw1-az3"]
  private_subnets = ["10.1.1.0/24", "10.1.2.0/24", "10.1.3.0/24"]
  public_subnets  = ["10.1.101.0/24", "10.1.102.0/24", "10.1.103.0/24"]

  enable_ipv6 = true

  enable_nat_gateway = false
  single_nat_gateway = false

  public_subnet_tags = {
    Name = "overridden-name-public"
  }

  tags = {
    Owner       = "Ametnes Devops"
    Environment = "Development"
    Location    = "Ireland"
  }

  vpc_tags = {
    Name = "demo-vpc"
  }
}

Initialize terraform in the folder ametnes-nodejs-sample.

terraform workspace new demo && terraform init

Run the terraform apply to create the resources. Feel free to run terraform plan if you'd like to view planned changes before apply.

...
module.vpc_euc1.aws_route_table_association.public[1]: Creating...
module.vpc_euc1.aws_route_table_association.private[0]: Creation complete after 0s [id=rtbassoc-05cecde9e705db87d]
module.vpc_euc1.aws_route_table_association.private[1]: Creation complete after 0s [id=rtbassoc-038faca3514feb6f8]
module.vpc_euc1.aws_route_table_association.public[0]: Creation complete after 1s [id=rtbassoc-06ce0a1689ded6a6f]
module.vpc_euc1.aws_route.private_ipv6_egress[1]: Creation complete after 1s [id=r-rtb-0cc80d4a84f99f54c2750132062]
module.vpc_euc1.aws_route_table_association.public[1]: Creation complete after 1s [id=rtbassoc-0376642ba86cd83ff]
module.vpc_euc1.aws_route.public_internet_gateway[0]: Creation complete after 1s [id=r-rtb-0b7a37d1c44a3667a1080289494]
module.vpc_euc1.aws_route.private_ipv6_egress[0]: Creation complete after 1s [id=r-rtb-04e87c56a77a9cc802750132062]
module.vpc_euc1.aws_route.public_internet_gateway_ipv6[0]: Creation complete after 1s [id=r-rtb-0b7a37d1c44a3667a2750132062]

Apply complete! Resources: 42 added, 0 changed, 0 destroyed.

Now we can query the terraform state in the Ametnes Postgres Service.

$pgcli -h postgres-a6bf.euw2.aws.cloud.ametnes.com -p 28427 -u somerandomuza initdb
$Password: <enter here>
initdb> \c networks
networks> SET search_path TO  tfstate;
networks> select provider, rtype, "instance"->'attributes'->'tags'->>'Location' as location, "instance"->'attributes'->>'cidr_block' as cidr_block from
(select r.resource->>'provider' as provider, r.resource->>'type' as rtype, json_array_elements(r.resource->'instances') as instance, r.resource->>'name' as name, r.resource from (select json_array_elements("data"::json->'resources') as resource from "states") as r where json_array_length(r.resource->'instances') > 0) as rsrc
where rtype = 'aws_vpc';
+-------------------+---------+------------+--------------+
| provider          | rtype   | location   | cidr_block   |
|-------------------+---------+------------+--------------|
| provider.aws.euw1 | aws_vpc | Ireland    | 10.1.0.0/16  |
| provider.aws.euc1 | aws_vpc | Frankfurt  | 10.0.0.0/16  |
+-------------------+---------+------------+--------------+
SELECT 2
Time: 0.042s

Warning

Don't for get to clean up resources you provisioned with terraform destroy.