Showing
1 changed file
with
151 additions
and
49 deletions
... | @@ -45,52 +45,154 @@ Or for the development version: | ... | @@ -45,52 +45,154 @@ Or for the development version: |
45 | pip install git+https://github.com/garnaat/kappa.git | 45 | pip install git+https://github.com/garnaat/kappa.git |
46 | 46 | ||
47 | 47 | ||
48 | -Getting Started | 48 | +Quick Start |
49 | ---------------- | 49 | +----------- |
50 | - | 50 | + |
51 | -Kappa is a command line tool. The basic command format is: | 51 | +To get a feel for how kappa works, let's take a look at a very simple example |
52 | - | 52 | +contained in the ``samples/simple`` directory of the kappa distribution. This |
53 | - kappa <path to config file> <command> [optional command args] | 53 | +example is so simple, in fact, that it doesn't really do anything. It's just a |
54 | - | 54 | +small Lambda function (written in Python) that accepts some JSON input, logs |
55 | -Where ``command`` is one of: | 55 | +that input to CloudWatch logs, and returns a JSON document back. |
56 | - | 56 | + |
57 | -* deploy - does whatever is required to deploy the current version of your | 57 | +The structure of the directory is: |
58 | - Lambda function such as creating/updating policies and roles and creating or | 58 | + |
59 | - updating the function itself | 59 | +``` |
60 | -* delete - delete the Lambda function, remove any event sources, delete the IAM | 60 | +simple/ |
61 | - policy and role | 61 | +├── _src |
62 | -* invoke - make a synchronous call to your Lambda function, passing test data | 62 | +│ ├── README.md |
63 | - and display the resulting log data | 63 | +│ ├── requirements.txt |
64 | -* invoke_async - make an asynchronous call to your Lambda function passing test | 64 | +│ ├── setup.cfg |
65 | - data. | 65 | +│ └── simple.py |
66 | -* dryrun - make the call but only check things like permissions and report | 66 | +├── _tests |
67 | - back. Don't actually run the code. | 67 | +│ └── test_one.json |
68 | -* tail - display the most recent log events for the function (remember that it | 68 | +└── kappa.yml.sample |
69 | - can take several minutes before log events are available from CloudWatch) | 69 | +``` |
70 | -* add_event_sources - hook up an event source to your Lambda function | 70 | + |
71 | -* update_event_sources - Update the event sources based on the information in | 71 | +Within the directory we see: |
72 | - your kappa config file | 72 | + |
73 | -* status - display summary information about functions, stacks, and event | 73 | +* kappa.yml.sample which is a sample YAML configuration file for the project |
74 | - sources related to your project. | 74 | +* _src which is a directory containing the source code for the Lambda function |
75 | - | 75 | +* _test which is a directory containing some test data |
76 | -The ``config file`` is a YAML format file containing all of the information | 76 | + |
77 | -about your Lambda function. | 77 | +The first step is to make a copy of the sample configuration file: |
78 | - | 78 | + |
79 | -If you use environment variables for your AWS credentials (as normally supported by boto), | 79 | + $ cd simple |
80 | -simply exclude the ``profile`` element from the YAML file. | 80 | + $ cp kappa.yml.simple kappa.yml |
81 | - | 81 | + |
82 | -An example project based on a Kinesis stream can be found in | 82 | +Now you will need to edit ``kappa.yml`` slightly for your use. The file looks |
83 | -[samples/kinesis](https://github.com/garnaat/kappa/tree/develop/samples/kinesis). | 83 | +like this: |
84 | - | 84 | + |
85 | -The basic workflow is: | 85 | +``` |
86 | - | 86 | +--- |
87 | -* Create your Lambda function | 87 | +name: kappa-simple |
88 | -* Create any custom IAM policy you need to execute your Lambda function | 88 | +environments: |
89 | -* Create some sample data | 89 | + dev: |
90 | -* Create the YAML config file with all of the information | 90 | + profile: <your profile here> |
91 | -* Run ``kappa <path-to-config> deploy`` to create roles and upload function | 91 | + region: <your region here> |
92 | -* Run ``kappa <path-to-config> test`` to invoke the function with test data | 92 | + policy: |
93 | -* Run ``kappa <path-to-config> deploy`` to upload new code for your Lambda | 93 | + resources: |
94 | - function | 94 | + - arn: arn:aws:logs:*:*:* |
95 | -* Run ``kappa <path-to-config> add_event_sources`` to hook your function up to the event source | 95 | + actions: |
96 | -* Run ``kappa <path-to-config> tail`` to see more output | 96 | + - "*" |
97 | + prod: | ||
98 | + profile: <your profile here> | ||
99 | + region: <your region here> | ||
100 | + policy: | ||
101 | + resources: | ||
102 | + - arn: arn:aws:logs:*:*:* | ||
103 | + actions: | ||
104 | + - "*" | ||
105 | +lambda: | ||
106 | + description: A very simple Kappa example | ||
107 | + handler: simple.handler | ||
108 | + runtime: python2.7 | ||
109 | + memory_size: 128 | ||
110 | + timeout: 3 | ||
111 | +``` | ||
112 | + | ||
113 | +The ``name`` at the top is just a name used for this Lambda function and other | ||
114 | +things we create that are related to this Lambda function (e.g. roles, | ||
115 | +policies, etc.). | ||
116 | + | ||
117 | +The ``environments`` section is where we define the different environments into | ||
118 | +which we wish to deploy this Lambda function. Each environment is identified | ||
119 | +by a ``profile`` (as used in the AWS CLI and other AWS tools) and a | ||
120 | +``region``. You can define as many environments as you wish but each | ||
121 | +invocation of ``kappa`` will deal with a single environment. Each environment | ||
122 | +section also includes a ``policy`` section. This is where we tell kappa about | ||
123 | +AWS resources that our Lambda function needs access to and what kind of access | ||
124 | +it requires. For example, your Lambda function may need to read from an SNS | ||
125 | +topic or write to a DynamoDB table and this is where you would provide the ARN | ||
126 | +([Amazon Resource Name](http://docs.aws.amazon.com/general/latest/gr/aws-arns-and-namespaces.html)) | ||
127 | +that identify those resources. Since this is a very simple example, the only | ||
128 | +resource listed here is for CloudWatch logs so that our Lambda function is able | ||
129 | +to write to the CloudWatch log group that will be created for it automatically | ||
130 | +by AWS Lambda. | ||
131 | + | ||
132 | +The ``lambda`` section contains the configuration information about our Lambda | ||
133 | +function. These values are passed to Lambda when we create the function and | ||
134 | +can be updated at any time after. | ||
135 | + | ||
136 | +To modify this for your own use, you just need to put in the right values for | ||
137 | +``profile`` and ``region`` in one of the environment sections. You can also | ||
138 | +change the names of the environments to be whatever you like but the name | ||
139 | +``dev`` is the default value used by kappa so it's kind of handy to avoid | ||
140 | +typing. | ||
141 | + | ||
142 | +Once you have made the necessary modifications, you should be ready to deploy | ||
143 | +your Lambda function to the AWS Lambda service. To do so, just do this: | ||
144 | + | ||
145 | +``` | ||
146 | +$ kappa deploy | ||
147 | +``` | ||
148 | + | ||
149 | +This assumes you want to deploy the default environment called ``dev`` and that | ||
150 | +you have named your config file ``kappa.yml``. If, instead, you called your | ||
151 | +environment ``test`` and named your config file foo.yml, you would do this: | ||
152 | + | ||
153 | +``` | ||
154 | +$ kappa --env test --config foo.yml deploy | ||
155 | +``` | ||
156 | + | ||
157 | +In either case, you should see output that looks something like this: | ||
158 | + | ||
159 | +``` | ||
160 | +$ kappa deploy | ||
161 | +deploying | ||
162 | +...deploying policy kappa-simple-dev | ||
163 | +...creating function kappa-simple-dev | ||
164 | +done | ||
165 | +$ | ||
166 | +``` | ||
167 | + | ||
168 | +So, what kappa has done is it has created a new Managed Policy called | ||
169 | +``kappa-simple-dev`` that grants access to the CloudWatch Logs service. It has | ||
170 | +also created an IAM role called ``kappa-simple-dev`` that uses that policy. | ||
171 | +And finally it has zipped up our Python code and created a function in AWS | ||
172 | +Lambda called kappa-simple-dev. | ||
173 | + | ||
174 | +To test this out, try this: | ||
175 | + | ||
176 | +``` | ||
177 | +$ kappa invoke _tests/test_one.json | ||
178 | +invoking | ||
179 | +START RequestId: 0f2f9ecf-9df7-11e5-ae87-858fbfb8e85f Version: $LATEST | ||
180 | +[DEBUG] 2015-12-08T22:00:15.363Z 0f2f9ecf-9df7-11e5-ae87-858fbfb8e85f {u'foo': u'bar', u'fie': u'baz'} | ||
181 | +END RequestId: 0f2f9ecf-9df7-11e5-ae87-858fbfb8e85f | ||
182 | +REPORT RequestId: 0f2f9ecf-9df7-11e5-ae87-858fbfb8e85f Duration: 0.40 ms Billed Duration: 100 ms Memory Size: 256 MB Max Memory Used: 23 MB | ||
183 | + | ||
184 | +Response: | ||
185 | +{"status": "success"} | ||
186 | +done | ||
187 | +$ | ||
188 | +``` | ||
189 | + | ||
190 | +We have just called our Lambda function, passing in the contents of the file | ||
191 | +``_tests/test_one.json`` as input to our function. We can see the output of | ||
192 | +the CloudWatch logs for the call and we can see the logging call in the Python | ||
193 | +function that prints out the ``event`` (the data) passed to the function. And | ||
194 | +finally, we can see the Response from the function which, for now, is just a | ||
195 | +hard-coded data structure returned by the function. | ||
196 | + | ||
197 | +That gives you a quick overview of kappa. To learn more about it, I recommend | ||
198 | +you check out the tutorial. | ... | ... |
-
Please register or login to post a comment