### Apply kloi Template (Bash) Source: https://github.com/daidokoro/kloi/blob/main/examples/templating/README.md This snippet shows the steps to clone the kloi repository, navigate to the templating example directory, and apply the template using the kloi CLI with a specified configuration file. ```bash git clone https://github.com/daidokoro/kloi.git cd kloi/examples/templating kloi apply -c config.star example ``` -------------------------------- ### Executing HTTP GET Requests with http.get (Starlark) Source: https://github.com/daidokoro/kloi/blob/main/README.md Shows how to use the `http.get` function to perform an HTTP GET request to a given URL. Examples include making requests with and without custom headers, and using the response body as the template source for a stack definition. ```Starlark # call with headers to get template template = http.get( headers={"some": "value"}, url='http://my.template.source') my_stack = stacks.new( name = 'my_stack', region = 'eu-west-1', template = template, ) ## --- OR my_stack = stacks.new( name = 'my_stack', region = 'eu-west-1', # call without headers template = http.get('http://my.template.source'), ) ``` -------------------------------- ### Generated CloudFormation Output - YAML Source: https://github.com/daidokoro/kloi/blob/main/README.md Example output of the 'kloi show' command, showing the resulting CloudFormation template in YAML format. It includes concrete VPC and Subnet resources generated from the Handlebars template based on the input data. ```yaml AWSTemplateFormatVersion: '2010-09-09' Transform: AWS::Serverless-2016-10-31 Resources: VPC0: Type: AWS::EC2::VPC Properties: CidrBlock: 10.10.10.0/24 EnableDnsSupport: 'true' EnableDnsHostnames: 'true' Tags: - Key: stack Value: kloi VPC1: Type: AWS::EC2::VPC Properties: CidrBlock: 10.10.10.1/24 EnableDnsSupport: 'true' EnableDnsHostnames: 'true' Tags: - Key: stack Value: kloi MySubnet: Type: 'AWS::EC2::Subnet' Properties: # AvailabilityZone: eu-central-1 VpcId: !Ref VPC0 CidrBlock: 10.10.10.0/24 MySubnet: Type: 'AWS::EC2::Subnet' Properties: # AvailabilityZone: eu-central-1 VpcId: !Ref VPC1 CidrBlock: 10.10.10.1/24 ``` -------------------------------- ### Delete kloi Resources (Bash) Source: https://github.com/daidokoro/kloi/blob/main/examples/templating/README.md This snippet demonstrates how to delete the resources that were previously applied using the kloi CLI, referencing the same configuration file and example name. ```bash kloi delete -c config.star example ``` -------------------------------- ### Executing HTTP POST Requests with http.post (Starlark) Source: https://github.com/daidokoro/kloi/blob/main/README.md Illustrates how to use the `http.post` function to send an HTTP POST request to a specified URL with a given payload (body). The example shows how to include a dictionary as the request body and use the response in a stack definition. ```Starlark # call with headers template = http.get( headers={"some": "value"}, url='http://my.template.source', body: {"some": "payload"}) my_stack = stacks.new( name = 'my_stack', region = 'eu-west-1', template = template, ) ## --- OR my_stack = stacks.new( name = 'my_stack', region = 'eu-west-1', # call without headers template = http.post( 'http://my.template.source', {"some": "payload"} ) ) ``` -------------------------------- ### Defining Stack with Handlebars Templating Values (Starlark) Source: https://github.com/daidokoro/kloi/blob/main/README.md Demonstrates defining a stack using `stacks.new` and providing a dictionary of `values`. These values are used by the Handlebars templating engine to expand variables within the template file specified by `os.open`. The example also shows setting stack capabilities. ```Starlark region = 'eu-central-1' values = { "cidrs": ["10.10.10.0/24", "10.10.10.1/24"], "subnet": { "region": region } } stack = stacks.new( name = 'ops', region = region, template = os.open('./template.yaml'), values = values, capabilities = [ 'CAPABILITY_AUTO_EXPAND', 'CAPABILITY_IAM' ] ) ``` -------------------------------- ### Reading File Contents with os.open (Python) Source: https://github.com/daidokoro/kloi/blob/main/README.md Reads the content of a file located at the specified path and returns it as a string. This function is part of the kloi `os` module and will cause kloi to panic if execution fails. The example shows reading a CloudFormation template file. ```python # read template file from file system. template = os.open('path/to/cfn/template.yaml') my_stack = stacks.new( name = 'my_stack', template = template, region = 'eu-west-1' ) ``` -------------------------------- ### Creating a New Kloi Stack (Python) Source: https://github.com/daidokoro/kloi/blob/main/README.md Initializes a new stack object for deployment using kloi. Requires stack name, region, and CloudFormation template. Optional parameters include template parameters, IAM capabilities, S3 bucket for large templates, templating values, and custom resources. Returns a stack object. ```python # read template file from file system. template = os.open('path/to/cfn/template.yaml') # define a stack my_stack = stacks.new( name = 'stack', region = 'eu-west-1', template = template, paramaeters = { 'VPCID': '123456789', 'PolicyName': 'MyPolicy' }, capabilities = [ 'CAPABILITY_IAM' ], bucket = 'bucket' ) ``` -------------------------------- ### Executing Host Commands with os.cmd (Starlark) Source: https://github.com/daidokoro/kloi/blob/main/README.md Demonstrates how to use the `os.cmd` function to execute shell commands on the host system. The output of the command is returned as a string, which can be used as input for other configurations, such as providing template content or values for stack definitions. ```Starlark # run script to generate cloudformation template template = os.cmd('my/template/generator.sh') template_values = os.cmd('my/values.sh') my_stack = stacks.new( name = 'my_stack', region = 'eu-west-1', template = template, values = values ) ``` -------------------------------- ### Showing Stack Template with Explicit Config - Shell Source: https://github.com/daidokoro/kloi/blob/main/README.md Executes the 'kloi show' command to display the generated CloudFormation template for a specific stack, using the provided configuration file. This is useful for verifying the template before deployment. ```sh $ kloi show --config ``` -------------------------------- ### Managing Stacks with KLOI_CONFIG - Shell Source: https://github.com/daidokoro/kloi/blob/main/README.md Demonstrates using 'kloi apply' and 'kloi delete' commands when the configuration file path is set via the 'KLOI_CONFIG' environment variable, allowing commands to be run without explicitly specifying the '--config' flag. ```sh $ kloi apply ``` ```sh $ kloi delete ``` -------------------------------- ### Generating CloudFormation Template - Shell Source: https://github.com/daidokoro/kloi/blob/main/README.md Executes the 'kloi show' command to generate and display the CloudFormation template for the 'ops' stack, using the configuration defined in 'config.star'. This command processes the template with the provided values. ```sh $ kloi show -c config.star ops ``` -------------------------------- ### AWS VPC/Subnet Template - Handlebars/YAML Source: https://github.com/daidokoro/kloi/blob/main/README.md Defines AWS VPC and Subnet resources using a Handlebars template. It iterates over a list of CIDR blocks ('cidrs') and conditionally creates subnets based on the presence of a 'subnet' object in the input values. ```handlebars AWSTemplateFormatVersion: '2010-09-09' Transform: AWS::Serverless-2016-10-31 Resources: {{#each cidrs}} VPC{{ @index }}: Type: AWS::EC2::VPC Properties: CidrBlock: {{ this }} EnableDnsSupport: 'true' EnableDnsHostnames: 'true' Tags: - Key: stack Value: kloi {{/each}} {{#if subnet }} {{#each cidrs}} MySubnet: Type: 'AWS::EC2::Subnet' Properties: # AvailabilityZone: {{ ../subnet.region }} VpcId: !Ref VPC{{@index}} CidrBlock: {{ this }} {{/each}} {{/if}} ``` -------------------------------- ### Managing Stacks with Explicit Config - Shell Source: https://github.com/daidokoro/kloi/blob/main/README.md Commands to deploy, update, or delete a CloudFormation stack using 'kloi apply' or 'kloi delete', explicitly specifying the configuration file path with the '--config' flag. ```sh $ kloi apply --config ``` ```sh $ kloi delete --config ``` -------------------------------- ### Enabling Debug Logging - Shell Source: https://github.com/daidokoro/kloi/blob/main/README.md Shows how to enable debug logs for kloi commands by setting the 'KLOI_LOG' environment variable. This can be done persistently in the current session or inline for a single command execution. ```sh $ export KLOI_LOG=debug ``` ```sh $ KLOI_LOG=debug kloi apply ``` -------------------------------- ### Reading Environment Variables with os.env (Starlark) Source: https://github.com/daidokoro/kloi/blob/main/README.md Illustrates how to use the `os.env` function to read the value of a specified environment variable. The returned string value can be directly used within configuration settings, such as defining the region for a stack. ```Starlark # use environment variable in config my_region = os.env('REGION') # use the value in a stack definition my_stack = stacks.new( name = "my_stack", region = my_region, template = '' ) ## --- OR my_stack = stacks.new( name = "my_stack", region = os.env('REGION'), template = '' ) ``` -------------------------------- ### Adding a Stack to Kloi Configuration (Python) Source: https://github.com/daidokoro/kloi/blob/main/README.md Registers a previously created stack object with the kloi configuration, making it available for management commands like `apply`. Requires a stack object created by `stacks.new`. Stacks must be added to be managed. ```python # create stack my_stack = stacks.new( name = "stack-name", template = "path/to/template.yml", parameters = { "param1": "value1", "param2": "value2" } ) # add stack stacks.add(my_stack) ``` -------------------------------- ### Adding Stack to List - Starlark Source: https://github.com/daidokoro/kloi/blob/main/README.md Adds a stack object to a list named 'stacks', typically within a configuration script (like config.star) used by kloi to define multiple stacks. ```starlark stacks.add(stack) ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.