Aller au contenu principal

Tester vos déploiements AWS avec localstack

· 5 minutes de lecture
Stéphane ROBERT
Consultant DevOps

Localstack une pépite ? Certainement. LocalStack est un container simulant une grande partie des services AWS. Il permet de développer et de tester ses déploiements, lambda, code terraform....

Installation

LocalStack est écrit en python et intègre beaucoup de librairies dont bien sûr celle d'AWS. Donc pour éviter d'éventuels conflits, je vous conseille de l'installer dans un environnement virtuel.

python3 -m venv ~/localstack
. ~/localstack/bin/activate
pip install localstack awscli-local

Démarrage de localstack

Maintenant que tout est prêt, nous pouvons lancer le provisionnement de la stack :

localstack start -d

     __                     _______ __             __
    / /   ____  _________ _/ / ___// /_____ ______/ /__
   / /   / __ \/ ___/ __ `/ /\__ \/ __/ __ `/ ___/ //_/
  / /___/ /_/ / /__/ /_/ / /___/ / /_/ /_/ / /__/ ,<
 /_____/\____/\___/\__,_/_//____/\__/\__,_/\___/_/|_|

 💻 LocalStack CLI 1.1.0

[07:03:10] starting LocalStack in Docker mode 🐳                                                                                             localstack.py:139
           preparing environment                                                                                                              bootstrap.py:667
           configuring container                                                                                                              bootstrap.py:675
           starting container                                                                                                                 bootstrap.py:681

Au bout de quelques minutes, nous obtenons un container avec toute une série de ports ouverts. En fait tous ses ports correspondent à chaque service AWS que localstack gère.

docker container ls --format "table {{.ID}}\t{{.Names}}\t{{.Ports}}"

CONTAINER ID   NAMES             PORTS
5a8a53c57d92   localstack_main   127.0.0.1:4510-4559->4510-4559/tcp, 127.0.0.1:4566->4566/tcp, 127.0.0.1:4571->4571/tcp, 127.0.0.1:12121->12121/tcp, 5678/tcp

Pour obtenir la liste des services :

❯ localstack status services
┏━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━┓
┃ Service                  ┃ Status      ┃
┡━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━┩
│ acm                      │ ✔ available │
│ apigateway               │ ✔ available │
│ cloudformation           │ ✔ available │
│ cloudwatch               │ ✔ available │
│ config                   │ ✔ available │
│ dynamodb                 │ ✔ available │
│ dynamodbstreams          │ ✔ available │
│ ec2                      │ ✔ available │
│ es                       │ ✔ available │
│ events                   │ ✔ available │
│ firehose                 │ ✔ available │
│ iam                      │ ✔ available │
│ kinesis                  │ ✔ available │
│ kms                      │ ✔ available │
│ lambda                   │ ✔ available │
│ logs                     │ ✔ available │
│ opensearch               │ ✔ available │
│ redshift                 │ ✔ available │
│ resource-groups          │ ✔ available │
│ resourcegroupstaggingapi │ ✔ available │
│ route53                  │ ✔ available │
│ route53resolver          │ ✔ available │
│ s3                       │ ✔ available │
│ s3control                │ ✔ available │
│ secretsmanager           │ ✔ available │
│ ses                      │ ✔ available │
│ sns                      │ ✔ available │
│ sqs                      │ ✔ available │
│ ssm                      │ ✔ available │
│ stepfunctions            │ ✔ available │
│ sts                      │ ✔ available │
│ support                  │ ✔ available │
│ swf                      │ ✔ available │
│ transcribe               │ ✔ available │
└──────────────────────────┴─────────────┘

Whaouuu

Test de création d'un container S3 en local

En fait local-stack c'est tout une série d'outils dont un terraform, pulumi, aws CDK, Serverless Framework. Testons terraform :

Pour l'utiliser il faut installer un wrapper de terraform :

pip install terraform-local

Sur le site de la documentation je retrouve un exemple de code terraform permettant de créer un bucket S3 :

provider "aws" {
  access_key                  = "test"
  secret_key                  = "test"
  region                      = "us-east-1"
  s3_force_path_style         = false
  skip_credentials_validation = true
  skip_metadata_api_check     = true
  skip_requesting_account_id  = true

  endpoints {
    apigateway     = "http://localhost:4566"
    apigatewayv2   = "http://localhost:4566"
    cloudformation = "http://localhost:4566"
    cloudwatch     = "http://localhost:4566"
    dynamodb       = "http://localhost:4566"
    ec2            = "http://localhost:4566"
    es             = "http://localhost:4566"
    elasticache    = "http://localhost:4566"
    firehose       = "http://localhost:4566"
    iam            = "http://localhost:4566"
    kinesis        = "http://localhost:4566"
    lambda         = "http://localhost:4566"
    rds            = "http://localhost:4566"
    redshift       = "http://localhost:4566"
    route53        = "http://localhost:4566"
    s3             = "http://s3.localhost.localstack.cloud:4566"
    secretsmanager = "http://localhost:4566"
    ses            = "http://localhost:4566"
    sns            = "http://localhost:4566"
    sqs            = "http://localhost:4566"
    ssm            = "http://localhost:4566"
    stepfunctions  = "http://localhost:4566"
    sts            = "http://localhost:4566"
  }
}

Allez on lance l'init et l'apply dans la foulée :

tflocal init

Initializing the backend...

Initializing provider plugins...
- Reusing previous version of hashicorp/aws from the dependency lock file
- Using previously-installed hashicorp/aws v3.75.2

Terraform has been successfully initialized!

You may now begin working with Terraform. Try running "terraform plan" to see
any changes that are required for your infrastructure. All Terraform commands
should now work.

If you ever set or change modules or backend configuration for Terraform,
rerun this command to reinitialize your working directory. If you forget, other
commands will detect it and remind you to do so if necessary.

Comme en vrai :

tflocal apply

...

Plan: 1 to add, 0 to change, 0 to destroy.

Do you want to perform these actions?
  Terraform will perform the actions described above.
  Only 'yes' will be accepted to approve.

  Enter a value: yes

aws_s3_bucket.test-bucket: Creating...
aws_s3_bucket.test-bucket: Creation complete after 1s [id=my-bucket]

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

Maintenant vérifions que nous avons notre bucket S3 :

```bash
awslocal s3 ls

2022-09-08 07:16:09 my-bucket

Plus loin

Je me sens pousser des ailes. Voilà un outil bien sympathique qui va me permettre de monter en compétences sur AWS et me mettre à développer des stacks en local ou des fonctions lambda en local. Finis les A/R.