Home > System Tutorial > LINUX > Use Ansible to automate the deployment of serverless applications

Use Ansible to automate the deployment of serverless applications

王林
Release: 2024-01-05 23:44:43
forward
948 people have browsed it
Introduction Ansible is designed to be the simplest deployment tool that actually works. This means it is not a complete programming language. You need to write a YAML template that defines the tasks and lists any tasks that need to be automated.

Most people think of Ansible as a more powerful "SSH in a for loop", and in simple usage scenarios this is true. But in fact Ansible is a task, not SSH. In many cases we connect via SSH, but it also supports things like Windows Remote Management (WinRM) on Windows machines, and the HTTPS API as a common language for cloud services.

In the cloud, Ansible can operate on two independent levels: control plane and instance resources. The control plane consists of everything that is not running on the operating system. This includes setting up your network, creating new instances, provisioning higher-level services like Amazon's S3 or DynamoDB, and everything you need to keep your cloud infrastructure secure and serving your customers.

Working on the instance is what you already know Ansible can do: starting and stopping services, templating configuration files, installing packages, and all operating system-related operations over SSH.

Now, what is serviceless? Depending on who you ask, serverless is either an infinite extension of the public cloud, or a completely new paradigm where everything is API calls and has never been done before.

Ansible takes the first view. Before "serviceless" was a technical term, users had to manage and configure EC2 instances, virtual private cloud (VPC) networks, and everything else. Serviceless is another step in the direction of managed services and works well with Ansible's agentless architecture.

Before we start the Lambda example, let's look at a simple configuration CloudFormation stack task:

- name: Build network
cloudformation:
stack_name: prod-vpc
state: present
template: base_vpc.yml
Copy after login

Writing a task like this only takes a few minutes, but it is the final semi-manual step involved in building the infrastructure - click "Create Stack" - this will put the playbook together with the others. Now your VPC is just another task to call upon when setting up a new region.

Since the cloud provider is the source of truth about what is happening in your account, Ansible has many ways to retrieve and filter and query running instances or networks using IDs, names, and other parameters. Taking the cloudformation_facts module as an example, we can get the subnet ID, network range, and other data from the template we just created.

- name: Pull all new resources back in as a variable
cloudformation_facts:
stack_name: prod-vpc
register: network_stack
Copy after login

For serverless applications, you will definitely need a Lambda function in addition to DynamoDB tables, S3 buckets, and anything else. Fortunately, by using the lambda module, a Lambda function can be created in the same way as the stack of the previous task:

- lambda:
name: sendReportMail
zip_file: "{{ deployment_package }}"
runtime: python3.6
handler: report.send
memory_size: 1024
role: "{{ iam_exec_role }}"
register: new_function
Copy after login

If you have other tools that you want to use to deliver serverless applications, this is also possible. The open source serverless framework has its own Ansible module that also works:

- serverless:
service_path: '{{ project_dir }}'
stage: dev
register: sls
- name: Serverless uses CloudFormation under the hood, so you can easily pull info back into Ansible
cloudformation_facts:
stack_name: "{{ sls.service_name }}"
register: sls_facts
Copy after login

This is not all you need, since the serverless project must also exist, where you will heavily define your functions and event sources. For this example, we will make a function that responds to HTTP requests. Serviceless frameworks use YAML as their configuration language (like Ansible), so this should look familiar.

# serverless.yml
service: fakeservice
provider:
name: aws
runtime: python3.6
functions:
main:
handler: test_function.handler
events:
- http:
path: /
method: get
Copy after login

At AnsibleFest, I'll cover this example and other in-depth deployment strategies to get the most out of the playbooks and infrastructure you already have, as well as new serverless practices. Whether you can get there or not, I hope these examples can get you started using Ansible, whether or not you have any services to manage.

AnsibleFest is a single-day conference that brings together hundreds of Ansible users, developers, and industry partners. Join us for product updates, inspiring conversations, technical deep dives, hands-on demos and networking all day long.

The above is the detailed content of Use Ansible to automate the deployment of serverless applications. For more information, please follow other related articles on the PHP Chinese website!

source:linuxprobe.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template