aws-cli¶
The available commands of aws-cli are a lot, and you can manage all that you need by those commands and bash scripts.
As an example, you can find some commands below.
aws-lambda¶
The available commands of aws-lambda are a lot, and you can manage all that you need by those commands and bash scripts.
By shell¶
The command for deploying a lambda is
export AWS_PROFILE=your-account
aws lambda create-function --function-name $name --handler $handler --runtime $runtime --role $role --zip-file fileb://code.zip
Where
- $handler is the name of the method within your code that Lambda calls to execute your function
- $runtime is the identifier for the language that you use in your code and it has many choices
- $role is the Amazon Resource Name (ARN) of the execution role of the lambda function
You can invoke the lambda function by command line
export AWS_PROFILE=your-account
aws lambda invoke --function-name $name outfile --payload '{"key":"value"}'
And also you can delete it by command line
export AWS_PROFILE=your-account
aws lambda delete-function --function-name $name
By a bash script¶
Below you can find an example of deployment of a lambda function by a bash custom script.
cd cli/lambda
git clone https://github.com/bilardi/aws-saving.git
export AWS_PROFILE=your-account
bash aws-lambda.sh deploy aws-saving/aws_saving
And for managing the other commands described above, you can use the same bash script
cd cli/lambda
bash aws-lambda.sh # print the commands list
Remember¶
When you need to
- change the permissions or other non-code attribute, it is better to delete and to redeploy the lambda function
- add the lambda function to a VPN, the deletion of that lambda function will be much slower because the system has to delete also the network objects
aws-ec2¶
The available commands of aws-ec2 are a lot, and you can manage all that you need by those commands and bash scripts.
By shell¶
The command for deploying an EC2 is
export AWS_PROFILE=your-account
aws ec2 run-instances --image-id $ami --count 1 --instance-type $type --subnet-id $sn --security-group-ids $sg # --user-data file://$file --key-name $key
The script return the instanceId of EC2 that you can use in the commands below
export AWS_PROFILE=your-account
export INSTANCE_ID=your-instance-id
for getting EC2 status,
aws ec2 describe-instance-status --instance-id INSTANCE_ID
for stopping EC2,
aws ec2 stop-instances --instance-ids INSTANCE_ID
for changing its instance type,
aws ec2 modify-instance-attribute --instance-id INSTANCE_ID --attribute instanceType --instance-type t3.small
# all instance T3 types are: t3.nano | t3.micro | t3.small | t3.medium | t3.large | t3.xlarge | t3.2xlarge
for changing its volume type,
aws ec2 modify-volume --volume-id INSTANCE_ID ---volume-type io1
# all volume types are: standard | io1 | gp2 | sc1 | st1
for starting EC2,
aws ec2 start-instances --instance-ids INSTANCE_ID
for deleting EC2,
aws ec2 terminate-instances --instance-ids INSTANCE_ID
By a bash script¶
Below you can find an example of deployment of a Minetest server by a bash custom script.
cd cli/ec2
curl -O https://raw.githubusercontent.com/bilardi/minetest/master/install.sh
export AWS_PROFILE=your-account
bash aws-ec2.sh deploy install.sh
And for managing the other commands described above, you can use the same bash script
cd cli/ec2
bash aws-ec2.sh # print the commands list
Remember¶
When you need to change
- the instance type or to terminate instance, before you have to stop the instance
- the volume type, it is not necessary to stop the instance, but if you stop it, the process will be more fast
aws-cloudformation¶
The available commands of aws-cloudformation are a lot, and you can manage all your stacks only these commands.
By shell¶
AWS CloudFormation service needs a template (and optionally parameters) for deploying a stack. A stack is a collection of resources that they are described in the template. The command for deploying a stack
export AWS_PROFILE=your-account
export STACK_NAME=your-stack-name
export PREFIX_STACK_FILENAME=your-filename
aws cloudformation create-stack --stack-name $STACK_NAME --template-body file://./templates/$PREFIX_STACK_FILENAME.yaml --parameters file://./config/$PREFIX_STACK_FILENAME.json --capabilities CAPABILITY_NAMED_IAM
When the stack will be created, you can get all information of the stack outputs that they are described in the template Outputs section. By the command below you can get the stack outputs
aws cloudformation describe-stacks --stack-name $STACK_NAME
For updating the stack after changes on parameters or templates files,
aws cloudformation update-stack --stack-name $STACK_NAME --template-body file://./templates/$PREFIX_STACK_FILENAME.yaml --parameters file://./config/$PREFIX_STACK_FILENAME.json --capabilities CAPABILITY_NAMED_IAM
for deleting the stack,
aws cloudformation delete-stack --stack-name $STACK_NAME
Below you can find a simple sample of deployment of a Minetest server by aws-cloudformation.
cd cli/cloudformation
export AWS_PROFILE=your-account
export EC2_STACK_NAME=minetest-server
export EC2_PREFIX_STACK_FILENAME=ec2
aws cloudformation create-stack --stack-name $EC2_STACK_NAME --template-body file://./templates/$EC2_PREFIX_STACK_FILENAME.yaml --parameters file://./config/$EC2_PREFIX_STACK_FILENAME.json --capabilities CAPABILITY_NAMED_IAM
Below you can find an example of deployment of a Minetest server by aws-cloudformation and what else you can manage.
cd cli/cloudformation
export AWS_PROFILE=your-account
export EC2_STACK_NAME=minetest-server-more
export EC2_PREFIX_STACK_FILENAME=ec2-more
aws cloudformation create-stack --stack-name $EC2_STACK_NAME --template-body file://./templates/$EC2_PREFIX_STACK_FILENAME.yaml --parameters file://./config/$EC2_PREFIX_STACK_FILENAME.json --capabilities CAPABILITY_NAMED_IAM
By a bash script¶
An ad hoc script is only necessary if you need to manage
- more parameters and only a few are dynamic variables like the name of other stacks
- an exception as for deleting a stack with a S3 bucket not empty or a dashboard configuration by external json file, as the sample that you can find in github.com/aws-samples
Below you can find an example of deployment of a Minetest server and a lambda function by a bash custom script.
cd cli/cloudformation
export AWS_PROFILE=your-account
bash aws-cloudformation.sh deploy plus
And for managing the other commands described above, you can use the same bash script
cd cli/cloudformation
bash aws-cloudformation.sh # print the commands list
Please, pay attention: in the config files, there are some identifiers that you need to change before running the bash script!
Remember¶
When you deploy your objects into the cloudformation stacks,
- you can take advantage of outputs section keys of others stacks with Fn::ImportValue
- you can avoid to hardcoding a password directly in the property or in the parameter configuration file by AWS::SecretsManager