Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PIOT-CFG-11-002: Update your python-components to generate a CSF zip file #161

Open
labbenchstudios opened this issue Mar 10, 2021 · 0 comments
Labels
additional Additional exercise configuration Configuration and / or setup requirement
Milestone

Comments

@labbenchstudios
Copy link
Contributor

labbenchstudios commented Mar 10, 2021

Description

  • These instructions describe how to use Maven to generate a cloud service function (CSF) zip file from within your python-components project.

NOTE: While the zip file generated may technically work with other cloud service providers, these configuration instructions will focus on AWS Lambda.

Review the README

  • Please see README.md for further information on, and use of, this content.
  • License for embedded documentation and source codes: PIOT-DOC-LIC

Estimated effort may vary greatly

  • The estimated level of effort for this exercise shown in the 'Estimate' section below is a very rough approximation. The actual level of effort may vary greatly depending on your development and test environment, experience with the requisite technologies, and many other factors.

Actions

NOTE: There are many ways to create a zip file of the Python components that will comprise an AWS Lambda. You can simply copy all of the requisite source files to a new directory, with relative paths retained, and then use your favorite zip-creation tool to create the zip file, or follow the instructions below to create a Maven assembly.

Using Maven

NOTE: These instructions provide general guidelines depicting how you can create the directives to handle packaging of the AWS Lambda code. You can embed the configuration directives within your pom.xml, or create a separate XML file that's referenced by your pom.xml. The examples in this instruction set focus on the latter. Keep in mind these are just examples - your configuration data and the properties embedded within your Maven XML files should be specific to YOUR implementation. Be sure to update them accordingly.

  • At the top level of your python-components directory, create a new pom.xml file. This will contain the project information as well as an assembly plugin directive to invoke another Maven configuration XML to create the zip file.
  • At the top level of your python-components directory, create a new directory named 'assembly'. This will contain the Maven configuration XML file(s) used to package your Lambda's. Create an XML file within the assembly directory - you can name this whatever you want - in the example below, I've named the file '

NOTE: This assumes you've already created your CSF modules (e.g., your AWS Lambda function in Python). The example below is specific to the CSF module ./programmingtheiot/csf/SensorDataHandlerFunction.py described in PIOT-CSF-11-001 and Programming the IoT - Chapter 11.

Sample Maven pom.xml (NOTE: Use your own properties)

<project xmlns="http://maven.apache.org/POM/4.0.0"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>programmingtheiot.cda</groupId>
	<artifactId>piot-cda</artifactId>
	<version>0.0.1</version>
	<name>Constrained Device App</name>
	<description>Programming the IoT - Constrained Device App - Packaging</description>
	<properties>
		<maven.compiler.source>11</maven.compiler.source>
		<maven.compiler.target>11</maven.compiler.target>
	</properties>
	<build>
		<plugins>
		<plugins>
			<plugin>
				<artifactId>maven-assembly-plugin</artifactId>
				<executions>
					<execution>
						<id>piot-aws-sensordatahandler</id>
						<phase>install</phase>
						<goals>
							<goal>single</goal>
						</goals>
						<configuration>
							<appendAssemblyId>false</appendAssemblyId>
							<descriptors>
								<descriptor>assembly/piot-aws-sensordatahandler-zip.xml</descriptor>
							</descriptors>
							<finalName>${project.artifactId}-aws-sensordatahandler-${project.version}</finalName>
						</configuration>
					</execution>
				</executions>
			</plugin>
		</plugins>
	</build>
</project>

Sample Maven Lambda/zip packaging XML (e.g., assembly/piot-aws-sensordatahandler-zip.xml)

<assembly
	xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2 http://maven.apache.org/xsd/assembly-1.1.2.xsd">
	<id>zip</id>
	<includeBaseDirectory>false</includeBaseDirectory>
	<formats>
		<format>zip</format>
	</formats>
	<fileSets>
		<fileSet>
			<directory>${project.basedir}/src/main/python</directory>
			<outputDirectory>/</outputDirectory>
			<includes>
				<include>/src/main/python/*.py</include>
			</includes>
		</fileSet>
		<fileSet>
			<directory>${project.basedir}/src/main/python/programmingtheiot/common</directory>
			<outputDirectory>/programmingtheiot/common</outputDirectory>
		</fileSet>
		<fileSet>
			<directory>${project.basedir}/src/main/python/programmingtheiot/data</directory>
			<outputDirectory>/programmingtheiot/data</outputDirectory>
		</fileSet>
	</fileSets>
	<files>
		<file>
			<source>${project.basedir}/src/main/python/programmingtheiot/csf/SensorDataHandlerFunction.py</source>
			<outputDirectory>/</outputDirectory>
		</file>
		<file>
			<source>${project.basedir}/src/main/python/programmingtheiot/__init__.py</source>
			<outputDirectory>/programmingtheiot</outputDirectory>
		</file>
	</files>
</assembly>

Estimate

  • Small to Medium

Tests

  • From a terminal / command line, change your directory to the top-level of python-components.
  • Execute mvn install
  • Check the directory target for a file with a name similar to piot-cda-aws-sensordatahandler-0.0.1.zip (yours may be different depending on your pom.xml properties)
  • Open the zip file and ensure the contents include the following from python-components:
    • All files from programmingtheiot\data, in the appropriate path
    • All files from programmingtheiot\config, in the appropriate path
    • The appropriate programmingtheiot\csf file (e.g., SensorDataHandlerFunction.py), at the top level
    • All __init__.py files (from programmingtheiot, data, common, and csf), in the appropriate paths
  • Follow the instructions in Programming the IoT - Chapter 11, specifically related to AWS.
  • If your zip file is created properly, you can simply import it within AWS as your AWS Lambda function for handling SensorData.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
additional Additional exercise configuration Configuration and / or setup requirement
Projects
Programming the IoT - Exercises Kanba...
  
Lab Module 11 - Cloud Integration
Development

No branches or pull requests

1 participant