Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to configure infrastructure as code in a programming language

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly explains "how to configure infrastructure as code with a programming language". The explanation in this article is simple, clear and easy to learn and understand. let's study and learn how to configure infrastructure as code in a programming language.

precondition

First, make sure you are ready to use Pulumi. Pulumi supports all major operating systems, so how you install its prerequisites depends on the operating system you use.

First, install the interpreter for your favorite programming language. I will use TypeScript, so I need to install the node binary. Please refer to the installation instructions for Node for information about your operating system. You can use Homebrew on Mac or Linux to install:

Brew install node

On Linux, you can use your usual package manager, such as apt or dnf.

$sudo dnf install nodejs

In either case, the result should be that the node binary is available in your $PATH. To verify that it is accessible, run:

Node-version

Next, install the Pulumi command line interface (CLI). You can find installation instructions for different operating systems in the Pulumi documentation. Using brew on Mac or Linux:

Brew install pulumi

In addition, you can also use installation scripts. First download and review it, and then execute it:

$curl-fsSL-- output pulumi_installer.sh https://get.pulumi.com/$ more pulumi_installer.sh$ sh. / pulumi_installer.sh

Again, what we want is to have pulumi binaries on your path. Check the version to make sure you are ready:

Pulumi versionv2.5.0 configuration Pulumi

Before you start configuring any infrastructure, give Pulumi a place to store its state.

Pulumi stores its state on the back end. The default backend is Pulumi's software-as-a-service (it has a free plan for individual users), but in this case, I use an alternative file backend. The file backend will create a file on your local file system to store the state:

Pulumi login-local

If you plan to share the project with others, the file background may not be a good starting point. Pulumi can also store its state in cloud object stores such as AWS S3. To use it, create an S3 bucket and log in:

Pulumi login-cloud-url s3://my-pulumi-state-bucket

Now that you are logged in to the status backend, you can create a project and a stack!

Before you start creating the Pulumi project, please understand the following Pulumi terms, which you will see in this tutorial.

Project

The project project is a directory that contains Pulumi.yaml files. This file contains the metadata that Pulumi needs to know in order to do its work. Example fields that can be found in the Pulumi.yaml file are:

Runtime (for example, Python, Node, Go, .net)

Project description (e.g. "my first Pulumi project")

Project name

Project is a loose concept that can meet your needs. In general, a project contains a series of resources that you want to provide and control. You can choose between small Pulumi projects with few resources, or large projects that contain all the resources you need. As you become more familiar with Pulumi, it will become clearer how you want to layout your project.

Stack

The Pulumi stack stack allows you to distinguish your Pulumi projects based on configurable values. A common use is to deploy a project to different environments, such as development or production environments, or to different regions, such as Europe, the Middle East and Africa, and the United States.

When getting started, you don't need a complex stack setting, so this walkthrough uses the default stack name dev.

Using TypeScript in IaC

You can use the convenient pulumi new command to start bootstrap a Pulumi project. The new command has a lot of flags and options that can help you get started with Pulumi, so continue to create your first project:

$pulumi new typescriptThis command will walk you through creating a new Pulumi project. Enter a value or leave blank to accept the (default), and press. Press ^ C at any time to quit. Project name: (pulumi) my-first-projectproject description: (A minimal TypeScript Pulumi program) My very first Pulumi programCreated project' my-first-project' Please enter your desired stack name.To create a stack in an organization, use the format / (e.g. `acmecorp/ dev`) .stack name: (dev) devCreated stack 'dev' Installing dependencies... > node scripts/postinstall added 82 packages from 126 contributors and audited 82 packages in 2.84s 13 packages are looking for funding run `npm fund` for details found 0 vulnerabilities Finished installing dependencies Your new project is ready to go! ✨ To perform an initial deployment, run 'pulumi up'

There's a lot going on here, and I'm going to start by saying:

The first part is to determine a template for your Pulumi project. I chose the general typescript option, but there are many options to choose from.

This new command grabs the template from your template library and copies the file locally, including runtime dependencies (in this case, package.json).

The new command installs these dependencies by running npm install from this directory. Npm install then downloads and installs everything you need to run the Pulumi program, in this case: the @ pulumi/pulumi NPM package.

You are ready to create your first resource!

Create your first cloud resource

A resource is something that is managed by the software life cycle of your infrastructure provider. The resource is typically a cloud provider object cloud provider object, such as S3 bucket. The provider of Pulumi handles Pulumi resources, and the provider is a specific cloud provider. Pulumi has about 40 providers available to you, but for your first resource, use one of the simplest: random provider random provider.

A random provider, as its name implies: it idempotently creates a random resource (for example, it could be a string) and stores it in a Pulumi state.

Use npm to add it to your Pulumi project as a dependency:

Npm install @ pulumi/random

The npm package manager downloads and installs the random provider package and installs it for you. Now you are ready to write your Pulumi program.

When you built your project earlier, the initial build process of Pulumi created an index.ts TypeScript file. Open it in your favorite integrated development environment (IDE) and add your first resource:

Import * as pulumi from "@ pulumi/pulumi"; import * as random from "@ pulumi/random"; const password = new random.RandomString (`password`, {length: 10})

If you are familiar with TypeScript or JavaScript, this will look very familiar because it is written in a programming language that you are familiar with. If you are using one of the other languages supported by Pulumi, the same is true. Here is the random resource before, but this time it is written in Python:

Import pulumi_random as random password = random.RandomString ("password", length=10)

A Pulumi project currently supports only one language, but each project can reference projects written in other languages, which is a useful technique for members of multilingual teams.

You have written your first Pulumi resource. Now you need to deploy it.

Leave the editor and go back to the command line. In your project directory, run pulumi up and watch something amazing happen:

Pulumi up Previewing update (dev): Type Name Plan + pulumi:pulumi:Stack my-first-project-dev create + └─ random:index:RandomString password create Resources: + 2 to create Do you want to perform this update? YesUpdating (dev): Type Name Status + pulumi:pulumi:Stack my-first-project-dev created + └─ random:index:RandomString password created Resources: + 2 created Duration: 2s Permalink: file:///Users/lbriggs/.pulumi/stacks/dev.json

Great, you have your first Pulumi resource! While you may enjoy this sense of achievement, unfortunately, this random resource is not that useful: it's just a random string, and you can't even see what it is. Solve this part of the problem first. Modify your previous program and add export to the constant you created:

Import * as pulumi from "@ pulumi/pulumi"; import * as random from "@ pulumi/random"; export const password = new random.RandomString (`password`, {length: 10})

Rerun pulumi up and look at the output:

Pulumi upPreviewing update (dev): Type Name Plan pulumi:pulumi:Stack my-first-project-dev Outputs: + password: {+ id: "& + r? {} J$J7" + keepers: output + length: 10 + lower: true + minLower: 0 + minNumeric: 0 + minSpecial: 0 + minUpper: 0 + number: true + overrideSpecial: output + result: "& + r? {} J$J7" + special: true + upper: true + urn: "urn:pulumi:dev::my-first-project::random:index/randomString:RandomString::password"} Resources: 2 unchanged Do you want to perform this update? YesUpdating (dev): Type Name Status pulumi:pulumi:Stack my-first-project-dev Outputs: + password: {+ id: "& + r? {} J$J7" + length: 10 + lower: true + minLower: 0 + minNumeric: 0 + minSpecial: 0 + minUpper: 0 + number: true + result: "& + r? {} J$J7" + special: true + upper: true + urn: "urn:pulumi:dev::my-first-project::random:index/randomString:RandomString::password"} Resources: 2 unchanged Duration: 1sPermalink: file:///Users/lbriggs/.pulumi/stacks/dev.json

Now you can see a randomly generated string under the result section of Outputs. You can now see that the resource you created has many properties.

All this is fine, but if you want to enjoy IaC, you have to provide something other than a random string. Try it.

Deploy a container

So far, you have experienced the startup of your Pulumi by installing dependencies and registering a simple random resource. Now deploy some physical infrastructure, albeit on your local machine.

First, add the @ pulumi/docker provider to your stack. Use the package manager of your choice to add it to the project:

Npm install @ pulumi/docker

You have pulled the Pulumi Docker provider package from npm, which means you can now create a Docker image in your project.

If you don't already have Docker installed on your machine, now is a great time to install it. The instructions will depend on your operating system, so check out the Docker installation page for information.

Open your favorite IDE again and run a Docker container. Modify your previous index.ts file to make it look like this:

Import * as pulumi from "@ pulumi/pulumi"; import * as random from "@ pulumi/random"; import * as docker from "@ pulumi/docker" Const password = new random.RandomString (`password`, {length: 10}) const container = new docker.Container (`password`, {image: 'hashicorp/http-echo', command: [pulumi.password`-text=Your super secret password is: ${password.result} `]], ports: [{internal: 5678, external: 5678,}]}) export const id = container.id

This creates a container and a Web server. The output from the Web server is your randomly generated string, in this case a password. Run this and see what happens:

Pulumi up Previewing update (dev): Type Name Plan pulumi:pulumi:Stack my-first-project-dev + └─ docker:index:Container my-password create Outputs: + id: output ~ password: {id: "& + r? {} J$J7" length: 10 lower: true MinLower: 0 minNumeric: 0 minSpecial: 0 minUpper: 0 number: true result: "& + r? {} J$J7" special: true upper: true urn: "urn:pulumi:dev::my-first-project::random:index/randomString:RandomString::password"} Resources: + 1 to create 2 unchanged Do you want to perform this update? YesUpdating (dev): Type Name Status pulumi:pulumi:Stack my-first-project-dev + └─ docker:index:Container my-password created Outputs: + id: "e73b34aeca34a64b72b61b0b9b8438637ce28853937bc359a1528ca99f49ddda" password: {id: "& + r? {} J$J7" length: 10 lower: true MinLower: 0 minNumeric: 0 minSpecial: 0 minUpper: 0 number: true result: "& + r? {} J$J7" special: true upper: true urn: "urn:pulumi:dev::my-first-project::random:index/randomString:RandomString::password"} Resources: + 1 created 2 unchanged Duration: 2sPermalink: file:/ / / Users/lbriggs/.pulumi/stacks/dev.json

You will notice that in the Outputs section, the value of your output has changed, it is just a Docker container ID. Check to see if your very simple password generator works:

Curl http://localhost:5678Your super secret password is: & + r? {} J$J7

okay! You just configured your first infrastructure with TypeScript.

A quick description of Pulumi output

You will notice that in the code that creates the Docker container, it uses a special pulumi.interpolate call. If you are familiar with TypeScript, you may wonder why you need to do this (because it is unique to Pulumi). There is an interesting reason for this.

When Pulumi creates a resource, there are some values that Pulumi doesn't know until the program executes. In Pulumi, these values are called Outputs. These Outputs can be seen in the code above, for example, in your first random resource, you use the export keyword to output the properties of the random resource, and you also output the container ID of the container you created.

Because Pulumi doesn't know the values of these Outputs until it executes, it needs a special helper to use them when manipulating strings. If you want to know more about this particular programming model, please watch this short video.

Thank you for your reading. the above is the content of "how to configure infrastructure as code in a programming language". After the study of this article, I believe you have a deeper understanding of how to configure infrastructure as code in a programming language, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report