Elastic Beanstalk 不额外收费,只需为存储和运行应用程序所需的 AWS 资源付费
EB CLI 是 Amazon Elastic Beanstalk 的命令行界面,它提供了可简化从本地存储库创建、更新和监控环境的交互式命令
安装eb cli
$ pip install virtualenv
$ virtualenv ebvenv
$ source venv/bin/activate
$ (ebvenv) pip install awsebcli --upgrade
$ $ eb --version
EB CLI 3.20.3 (Python 3.7.1)
帮助信息
commands:
abort Cancels an environment update or deployment.
appversion Listing and managing application versions
clone Clones an environment.
codesource Configures the code source for the EB CLI to use by default.
config Modify an environment's configuration. Use subcommands to manage saved configurations.
console Opens the environment in the AWS Elastic Beanstalk Management Console.
create Creates a new environment.
deploy Deploys your source code to the environment.
events Gets recent events.
health Shows detailed environment health.
init Initializes your directory with the EB CLI. Creates the application.
labs Extra experimental commands.
list Lists all environments.
local Runs commands on your local machine.
logs Gets recent logs.
open Opens the application URL in a browser.
platform Commands for managing platforms.
printenv Shows the environment variables.
restore Restores a terminated environment.
scale Changes the number of running instances.
setenv Sets environment variables.
ssh Opens the SSH client to connect to an instance.
status Gets environment information and status.
swap Swaps two environment CNAMEs with each other.
tags Allows adding, deleting, updating, and listing of environment tags.
terminate Terminates the environment.
upgrade Updates the environment to the most recent platform version.
use Sets default environment.
初始化
初始化eb环境,可以指定凭证profile
$ mkdir test-beangolang && cd test-beangolang
$ eb init
$ #eb init --profile profilename
初始化使用默认的aws凭证,要求选择region,application,ssh,platform(此处选择go)等。新的application默认以当前文件夹名称创建。
初始化完成后在本地生成如下文件。其中config.yml
包含配置信息
$ tree -a
.
├── .elasticbeanstalk
│ └── config.yml
└── .gitignore
$ cat .elasticbeanstalk/config.yml
branch-defaults:
default:
environment: null
group_suffix: null
global:
application_name: test-beangolang
branch: null
default_ec2_keyname: dev-key
default_platform: Go 1 running on 64bit Amazon Linux 2
default_region: cn-north-1
include_git_submodules: true
instance_profile: null
platform_name: null
platform_version: null
profile: null
repository: null
sc: null
workspace_type: Application
初始化之后只会创建application,不会启动environment(即实例,负载均衡器,数据库等资源)
高级配置
如果
.ebignore
不存在,但存在.gitignore
,EB CLI 将忽略.gitignore
中指定的文件。如果存在.ebignore
,EB CLI 将不会读取.gitignore
eb cli工具的凭证优先级
部署构建而非文件夹
创建环境
$ eb create
环境初始化会选择环境name,dns,lb,是否下载sample,最后启动环境,启动过程中会输出日志
最终所有资源都会通过创建cloudformation的方式进行创建
Would you like to enable Spot Fleet requests for this environment? (y/N): n
NOTE: The current directory does not contain any source code. Elastic Beanstalk is launching the sample application instead.
Do you want to download the sample application into the current directory? (Y/n): y
INFO: Downloading sample application to the current directory.
INFO: Download complete.
Environment details for: test-beangolang-dev
Application name: test-beangolang
Region: cn-north-1
Deployed Version: Sample Application
Environment ID: e-254v23ad2m
Platform: arn:aws-cn:elasticbeanstalk:cn-north-1::platform/Go 1 running on 64bit Amazon Linux 2/3.6.1
Tier: WebServer-Standard-1.0
CNAME: test-beangolang-dev.cn-north-1.eb.amazonaws.com.cn
Updated: 2022-11-13 15:35:24.287000+00:00
Printing Status:
2022-11-13 15:35:23 INFO createEnvironment is starting.
2022-11-13 15:35:24 INFO Using elasticbeanstalk-cn-north-1-xxxxxxxxxxxx as Amazon S3 storage bucket for environment data.
2022-11-13 15:35:46 INFO Created target group named: arn:aws-cn:elasticloadbalancing:cn-north-1:xxxxxxxxxxxx:targetgroup/awseb-test-bea-default-5gkjd/6de4b45758a2fab6
2022-11-13 15:35:46 INFO Created security group named: awseb-e-254v23ad2m-stack-AWSEBSecurityGroup-1X8RQFJTFG3WA
2022-11-13 15:36:03 INFO Created Auto Scaling launch configuration named: awseb-e-254v23ad2m-stack-AWSEBAutoScalingLaunchConfiguration-vWQ0HP7XfOUA
2022-11-13 15:36:03 INFO Created Load Balancer listener rule named: arn:aws-cn:elasticloadbalancing:cn-north-1:xxxxxxxxxxxx:listener-rule/app/main-alb/17ec2f283414a86a/80c5154ba2598551/d2ff9a73b3344c39
-- Events -- (safe to Ctrl+C)
下载sample程序,一个简单的go web程序
$ tree
.
├── application.go
├── cron.yaml
└── public
└── index.html
查看当前状态和健康状况
$ eb status
Environment details for: test-beangolang-dev
Application name: test-beangolang
Region: cn-north-1
Deployed Version: Sample Application
Environment ID: e-254v23ad2m
Platform: arn:aws-cn:elasticbeanstalk:cn-north-1::platform/Go 1 running on 64bit Amazon Linux 2/3.6.1
Tier: WebServer-Standard-1.0
CNAME: test-beangolang-dev.cn-north-1.eb.amazonaws.com.cn
Updated: 2022-11-13 15:37:06.756000+00:00
Status: Launching
Health: Grey
$ eb health
查看事件输出,包括环境构建,重新部署,配置修改等事件日志,和图形控制台一致
$ eb evnets
查看控制台