Python Hello World Sample for AWS CodeBuild
This Python sample tests whether an internal variable in code contains the string
Hello world!. It produces as build output a single file named
HelloWorld.py.
Important
Running this sample may result in charges to your AWS account. These include possible charges for AWS CodeBuild and for AWS resources and actions related to Amazon S3, AWS KMS, and CloudWatch Logs. For more information, see AWS CodeBuild Pricing, Amazon S3 Pricing, AWS Key Management Service Pricing, and Amazon CloudWatch Pricing.
Running the Sample
To run this sample:
-
Create the files as described in the Directory Structure and Files sections of this topic, and then upload them to an Amazon S3 input bucket or an AWS CodeCommit, GitHub, or Bitbucket repository.
Important
Do not upload
, just the files inside of(root directory name).(root directory name)If you are using an Amazon S3 input bucket, be sure to create a ZIP file that contains the files, and then upload it to the input bucket. Do not add
to the ZIP file, just the files inside of(root directory name).(root directory name) -
Create a build project, run the build, and view related build information by following the steps in Run AWS CodeBuild Directly.
If you use the AWS CLI to create the build project, the JSON-formatted input to the
create-projectcommand might look similar to this. (Replace the placeholders with your own values.){ "name": "sample-python-project", "source": { "type": "S3", "location": "codebuild-region-ID-account-ID-input-bucket/PythonSample.zip" }, "artifacts": { "type": "S3", "location": "codebuild-region-ID-account-ID-output-bucket", "packaging": "ZIP", "name": "PythonOutputArtifact.zip" }, "environment": { "type": "LINUX_CONTAINER", "image": "aws/codebuild/python:3.5.2", "computeType": "BUILD_GENERAL1_SMALL" }, "serviceRole": "arn:aws:iam::account-ID:role/role-name", "encryptionKey": "arn:aws:kms:region-ID:account-ID:key/key-ID" } -
To get the build output artifact, open your Amazon S3 output bucket.
-
Download the
file to your local computer or instance, and then extract the contents of the file. In the extracted contents, get thePythonOutputArtifact.zipHelloWorld.pyfile.
Directory Structure
This sample assumes this directory structure.
(root directory name)|-- buildspec.yml |-- HelloWorld.py `-- HelloWorld_tst.py
Files
This sample uses these files.
buildspec.yml (in )
(root directory
name)
version: 0.2 phases: build: commands: - echo Build started on `date` - echo Compiling the Python code... - python HelloWorld_tst.py post_build: commands: - echo Build completed on `date` artifacts: files: - HelloWorld.py
HelloWorld.py (in )
(root directory
name)
class HelloWorld: def __init__(self): self.message = 'Hello world!'
HelloWorld_tst.py (in )
(root directory
name)
import unittest from HelloWorld import HelloWorld class MyTestCase(unittest.TestCase): def test_default_greeting_set(self): hw = HelloWorld() self.assertEqual(hw.message, 'Hello world!') if __name__ == '__main__': unittest.main()
Related Resources
-
For more information about getting started with AWS CodeBuild, see Getting Started with AWS CodeBuild.
-
For more information about troubleshooting problems with AWS CodeBuild, see Troubleshooting Your VPC SetupTroubleshooting AWS CodeBuild.
-
For more information about limits in AWS CodeBuild, see Limits for AWS CodeBuild.


