aws cdn_摩托 模拟AWS

aws cdn

If your application uses Amazon Web Services, python library moto is the perfect thing for that.

如果您的应用程序使用Amazon Web Services ,那么pythonmoto是完美的选择。

Full list of implementation coverage is here.

实施覆盖范围的完整列表在这里

I found the repo of Hugo Picado on Github — moto-server. Ready to start'n'use image. The only nuance is that after launch, no AWS resources will be created.

我在Github上找到了Hugo Picado的仓库— moto-server 。 准备开始使用图像。 唯一的区别是,启动后将不会创建任何AWS资源。

Well, it's pretty easy to fix it.

好吧,很容易修复它。

Whereas we must define env variable

然而,我们必须定义环境变量

MOTO_SERVICE (MOTO_SERVICE)

to specify type of resource at launch, all we have left is to define resource creation.

为了在启动时指定资源类型,剩下的就是定义资源创建。

Update start.sh:

更新start.sh

Replace

更换

moto_server $MOTO_SERVICE -H $MOTO_HOST -p $MOTO_PORT

with

if [ -f /opt/init/bootstrap.py ]; then
  moto_server $MOTO_SERVICE -H $MOTO_HOST -p $MOTO_PORT & (sleep 5 && echo "Executing bootstrap script." && python /opt/init/bootstrap.py)
else
  moto_server $MOTO_SERVICE -H $MOTO_HOST -p $MOTO_PORT
fi
wait

The file should be like:

该文件应类似于:

start.sh (start.sh)

#!/bin/sh

# validate required input
if [ -z "$MOTO_SERVICE" ]; then
  echo "Please define AWS service to run with Moto Server (e.g. s3, ec2, etc)"
  exit 1
fi

# setting defaults for optional input
if [ -z "$MOTO_HOST" ]; then
  MOTO_HOST="0.0.0.0"
fi

if [ -z "$MOTO_PORT" ]; then
  MOTO_PORT="5000"
fi

echo "Starting service $MOTO_SERVICE at $MOTO_HOST:$MOTO_PORT"

if [ -f /opt/init/bootstrap.py ]; then
  moto_server $MOTO_SERVICE -H $MOTO_HOST -p $MOTO_PORT & (sleep 5 && echo "Executing bootstrap script." && python /opt/init/bootstrap.py)
else
  moto_server $MOTO_SERVICE -H $MOTO_HOST -p $MOTO_PORT
fi
# Prevent container from exiting when bootstrap.py finishing
wait

Build new image and push it to your registry.

构建新映像并将其推送到注册表。

Futher, lets make a script of resource initialization, using the library to work with AWS — boto3. E.g.SWF domain:

此外,让我们使用该库与AWS-boto3一起创建资源初始化脚本。 例如SWF网域

bootstrap_swf.py (bootstrap_swf.py)

import boto3
from botocore.exceptions import ClientError
import os

os.environ["AWS_ACCESS_KEY_ID"] = "fake"
os.environ["AWS_SECRET_ACCESS_KEY"] = "fake"

client = boto3.client('swf', region_name='us-west-2', endpoint_url='http://localhost:5000')

try:
    client.register_domain(
        name='test-swf-mock-domain',
        description="Test SWF domain",
        workflowExecutionRetentionPeriodInDays="10"
    )
except ClientError as e:
    print "Domain already exists: ", e.response.get("Error", {}).get("Code")

response = client.list_domains(
    registrationStatus='REGISTERED',
    maximumPageSize=123,
    reverseOrder=True|False
)

print 'Ready'

Logically:

逻辑上:

  • Mount our bootstrap script to /opt/init/bootstrap.py

    将我们的引导脚本安装到/opt/init/bootstrap.py

  • If file mounted — it will be executed

    如果文件已挂载-将被执行
  • If not — the only moto-server will be launched

    如果没有,则将启动唯一的moto服务器

So, we can mock the whole resource by just run only one container:

因此,我们可以只运行一个容器来模拟整个资源:

docker run --name swf -d \
    -e MOTO_SERVICE=swf \
    -e MOTO_HOST=0.0.0.0 \
    -e MOTO_PORT=5000 \
    -p 5001:5000 \
    -v /tmp/bootstrap_swf.py:/opt/init/bootstrap.py \
    -i awesome-repo.com/moto-server:latest

Let's try in the interactive mode:

让我们尝试以交互方式:

It works!

有用!

Thus, we can create docker-compose.yml which can help save time for testing changes:

因此,我们可以创建docker-compose.yml,它可以帮助节省测试更改的时间:

docker-compose.yml (docker-compose.yml)

version: '3'
services:
  s3:
    image: picadoh/motocker
    environment:
      - MOTO_SERVICE=s3
      - MOTO_HOST=10.0.1.2
    ports:
      - "5002:5000"
    networks:
      motonet:
        ipv4_address: 10.0.1.2
    volumes:
      - /tmp/bootstrap_s3.py:/opt/init/bootstrap.py
  swf:
    image: picadoh/motocker
    environment:
      - MOTO_SERVICE=swf
      - MOTO_HOST=10.0.1.3
    ports:
      - "5001:5000"
    networks:
      motonet:
        ipv4_address: 10.0.1.3
    volumes:
      - /tmp/bootstrap_swf.py:/opt/init/bootstrap.py
  ec2:
    image: picadoh/motocker
    environment:
      - MOTO_SERVICE=ec2
      - MOTO_HOST=10.0.1.4
    ports:
      - "5003:5000"
    networks:
      motonet:
        ipv4_address: 10.0.1.4
    volumes:
      - /tmp/bootstrap_ec2.py:/opt/init/bootstrap.py
networks:                             
  motonet:                          
    driver: bridge                
    ipam:                         
      config:                       
        - subnet: 10.0.0.0/16

By the way, we can use this approach not only on the developer's laptop. Preliminary tests with mocks after build will help us to get rid of possible problems on the dev* environments.

顺便说一句,我们不仅可以在开发人员的笔记本电脑上使用这种方法。 构建后对模拟进行的初步测试将帮助我们摆脱开发环境上的可能问题。

Links:

链接:

Motocker repo — github.com/picadoh/motocker

Motocker回购协议— github.com/picadoh/motocker

Moto repo — github.com/spulec/moto

Moto回购-github.com/spulec/moto

Boto3 Docs — boto3.amazonaws.com/v1/documentation/api/latest/index.html

Boto3文件-boto3.amazonaws.com/v1/documentation/api/latest/index.html

翻译自: https://habr.com/en/post/454892/

aws cdn

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值